October 2016: Qingdao Network Competition --- Family View (AC automation), 2016 --- family
Question Link
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5880
Problem DescriptionSteam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and social networking services. A family view can help you to prevent your children access to some content which are not suitable for them.
Take an MMORPG game as an example, given a sentence T, and a list of forbidden words {P}, your job is to use '*' to subsititute all the characters, which is a part of the substring matched with at least one forbidden word in the list (case-insensitive ).
For example, T is: "I love Beijing's Tiananmen, the sun rises over Tiananmen. Our great leader Chairman Mao, he leades us marching on ."
And {P} is: {"tiananmen", "eat "}
The result shoshould be: "I love Beijing's **********, the sun rises over *********. our gr *** leader Chairman Mao, he leades us marching on."
InputThe first line contains the number of test cases. For each test case:
The first line contains an integer n, represneting the size of the forbidden words list P. each line of the next n lines contains a forbidden words Pi (1 ≤ | Pi | ≤ 1000000, Σ | Pi | ≤ 1000000) where Pi only contains lowercase letters.
The last line contains a string T (| T | ≤ 1000000 ).
OutputFor each case output the sentence in a line.
Sample Input13 trumprioDonald John Trump (born June 14,194 6) is an American businessman, television personality, author, politician, and the Republican Party nominee for President of the United States in the 2016 election. he is chairman of The Trump Organization, which is the principal holding company for his real estate ventures and other business interests.
Sample OutputD * nald J * hn ***** (B * rn June 14,194 6) is an Ame ** can businessman, televisi * n pers * nality, auth * r, p * litician, and the Republican Party n * minee f * r President * f the United States in the 2016 electi * n. he is chairman * f The ******* rganizati * n, which is the p ** ncipal h * lding c * mpany f * r his real estate ventures and * ther business interests.
Source2016 ACM/ICPC Asia Regional Qingdao Online
RecommendWange2014 | We have carefully selected several similar problems for you: 5901 5899 5898 5897 5896 question: there are n sensitive words consisting of lower-case letters, and now a main string is provided, it is required to replace the sensitive word "*" and then output the main string. Train of Thought: To apply the AC automatic machine template, a quick solution is to define a tag array v [maxn]. the start position v [start] ++ and end position v [end + 1] of sensitive words in the primary string -- when outputting the primary string, sum + = v [I]. If sum> 0, "*" is output; otherwise, "*" is output. The data for this question is large, and many people keep popping up the memory. So do I ~ I used the linked list when I created the trie tree, so each time I inserted a new node, I opened a Node space. After each group of data is calculated, I did not clean up the space, so no matter how you change the memory, it's time to find out, alas! So be sure to clear the memory! The Code is as follows:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # define N 1000005 using namespace std; char str [1000005]; int v [1000005]; int head, tail; struct node {node * fail; node * next [26]; int count; node () {fail = NULL; count = 0; for (short I = 0; I <26; I ++) next [I] = NULL;} * q [N]; node * root; void insert (char * str) // create Trie {int temp, len; node * p = root; len = strlen (str); for (int I = 0; I <len; I ++) {temp = Str [I]-'A'; if (p-> next [temp] = NULL) p-> next [temp] = new node (); p = p-> next [temp];} p-> count = len;} void setfail () // initialize the fail pointer, BFS {q [tail ++] = root; while (head! = Tail) {node * p = q [head ++]; node * temp = NULL; for (short I = 0; I <26; I ++) if (p-> next [I]! = NULL) {if (p = root) // The fail at the first letter must point to the root p-> next [I]-> fail = root; else {temp = p-> fail; // failed pointer while (temp! = NULL) // if the condition is NULL, the match is NULL or the match {if (temp-> next [I] is found. = NULL) // find the match {p-> next [I]-> fail = temp-> next [I]; break;} temp = temp-> fail ;} if (temp = NULL) // if it is NULL, match p-> next [I]-> fail = root ;} q [tail ++] = p-> next [I]; // enter the team; }}} void query () {node * p = root; int len = strlen (str); for (int I = 0; I <len; I ++) {int index; if (str [I]> = 'A' & str [I] <= 'Z') index = str [I]-'A '; else if (str [I]> = 'A' & str [I] <= 'Z') index = str [I]-'A '; else {p = root; continue;} while (p-> next [index] = NULL & p! = Root) // jump failure pointer p = p-> fail; p = p-> next [index]; if (p = NULL) p = root; node * temp = p; // p does not move. temp calculates the suffix string while (temp! = Root) {if (temp-> count> 0) {v [I-temp-> count + 1] ++; v [I + 1] --; break ;} temp = temp-> fail;} return;} int main () {int T, num; scanf ("% d", & T); while (T --) {for (int I = 0; I <tail; I ++) free (q [I]); memset (v, 0, sizeof (v )); head = tail = 0; root = new node (); scanf ("% d", & num); getchar (); for (int I = 0; I <num; I ++) {gets (str); insert (str) ;}setfail (); gets (str); int len = strlen (str), sum = 0; query (); for (int I = 0; I <len; I ++) {sum + = v [I]; if (sum <= 0) printf ("% c", str [I]); else printf ("*");} puts ("");} return 0 ;}