Hdu 2222 Keywords Search (AC)
Keywords SearchTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission (s): 42138 Accepted Submission (s): 13289
Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you shoshould tell me how many keywords will be match.
Input First line will contain one integer means how many cases will follow.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'Z', and the length will not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output Print how many keywords are contained in the description.
Sample Input
15shehesayshrheryasherhs
Sample Output
3
Question: I will give you a lot of model strings, and then give you a mother string and ask how many strings are in the mother string. The Mode strings may be the same, and there may be multiple.
Train of Thought: multi-mode string matching. The entry question of AC automatic machine is the classic application of bfs and dp, and the code is clever.
Code:
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 1000010; const int bsz = 26; typedef long ll; char txt [1000010]; bool vis [510]; int ans; struct Trie {int ch [maxn] [bsz], val [maxn], sz, cnt [maxn]; // ch stores the word cnt corresponding to the Trie val-node. The number of words ending with the node. int f [maxn], last [maxn]; // f-Mismatch pointer last-suffix link int newnode () {val [sz] = 0; cnt [sz] = 0; memset (ch [sz],-1, sizeof ch [sz]); return sz ++;} void init () {sz = 0; newnode () ;} int idx (char c) // take the c label for details. {return c-'A';} void Insert (char * st, int id) {int u = 0, n = strlen (st), c, I; for (I = 0; I
Q; f [0] = 0; for (I = 0; I