Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 85068 Accepted Submission(s): 29646
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 should tell me how many keywords will be match.
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 should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
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 be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
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 be 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
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
#include<iostream> #include<string.h> #include<string> #include<algorithm> #include<queue> #include<set> #define ll long long using namespace std; int tree[400005][26],vis[400005],fail[400005]; int t,n,cnt,id,root,num=0; string s,ss; void insert()//建树 { root=0; For ( int I = 0 ; S [I]; I ++ ) { ID = S [I] - ' A ' ; IF (Tree [the root] [ID] == 0 ) Tree [the root] [ID] = ++ NUM; the root = Tree [the root] [ID]; } VIS [the root] ++; // words ending flag } void build () // Construction mismatch pointer { Queue < int > P; for ( int I = 0 ; i <26 is ; I ++ ) { IF (Tree [ 0 ] [I]) // second row all the letters appeared mismatch pointer to the root node 0 { Fail [Tree [ 0 ] [I]] = 0 ; P. Push (Tree [ 0 ] [I]); } } the while (! p.empty ()) { the root = p.front (); p.pop (); for ( int I = 0 ; I < 26 is ; I ++ ) { IF (Tree [the root] [I] == 0 ) // no contribution, the absence of the letter Continue ; p.push (Tree [the root] [I]); int FA = Fail [the root]; // FA parent node is the while (FA Tree && [FA] [I] == 0 ) // FA is not 0, and child node is not the letter fa fa = Fail [fa]; // child node of the parent node is determined to continue the letter fa there are no Fail [Tree [the root] [I]] = Tree [fa] [I]; // Construction of a pointer to find a mismatch } } } int Search ( String SS) // find { the root = 0 , CNT = 0 ; for ( int I = 0; SS [I]; I ++ ) { ID = SS [I] - ' A ' ; the while (the root && Tree [the root] [ID] == 0 ) // mismatch transfer the root = Fail [the root]; the root = Tree [the root ] [ID]; int TEMP = the root; the while (VIS [TEMP]) { CNT = CNT + VIS [TEMP]; VIS [TEMP] = 0 ; // clear the flag, to avoid duplication TEMP = Fail [TEMP]; } } return cnt; } int main() { cin>>t; while(t--) { memset(tree,0,sizeof(tree)); memset(vis,0,sizeof(vis)); cin>>n; for(int i=0;i<n;i++) { cin>>s; insert(); } build(); cin>>ss;//文本串 cout<<search(ss)<<endl; } }