HDU2222(Keywords Search,AC自动机)
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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
写的第一道AC自动机。。同样是裸题,但是。。。
我TM的交了35次TTTTTTTTTTTTTTTTTT_______TTTTTTTTTTTTTTTTTTT
....恩。。。在match函数里。。
int id = st[i]-'a',cur=ch[cur][id]; 与 int id = st[i]-'a';cur=ch[cur][id];
一个字符的区别。。艹
1 #include <set> 2 #include <queue> 3 #include <vector> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 using namespace std; 10 const int SONS = 26; 11 const int MAXN = 500100; 12 #define For(i,n) for(int i=1;i<=n;i++) 13 #define For0(i,n) for(int i=0;i<n;i++) 14 #define Rep(i,l,r) for(int i=l;i<=r;i++) 15 int T,ans,n; 16 char goal[MAXN*2]; 17 18 struct trie{ 19 int sz,ch[MAXN][SONS],v[MAXN],next[MAXN],Q[MAXN]; 20 void clr(){sz=0;memset(ch[0],0,sizeof(ch[0]));} 21 void insert(char *st){ 22 int len=strlen(st),cur=0; 23 For0(i,len){ 24 int id=st[i]-'a'; 25 if(!ch[cur][id]) { 26 ch[cur][id]=++sz; 27 v[sz]=0;memset(ch[sz],0,sizeof(ch[sz])); 28 } 29 cur=ch[cur][id]; 30 } 31 v[cur]++; 32 } 33 void BuildNext(){ 34 int l=0,r=0; 35 For0(i,SONS) 36 if(ch[0][i]) { 37 Q[++r]=ch[0][i]; 38 next[Q[r]]=0; 39 } 40 while(l<r){ 41 int cur=Q[++l]; 42 For0(i,SONS) 43 if(!ch[cur][i]) ch[cur][i]=ch[next[cur]][i]; 44 else{ 45 Q[++r]=ch[cur][i]; 46 next[ch[cur][i]]=ch[next[cur]][i]; 47 } 48 } 49 } 50 void match() { 51 int cur = 0,len=strlen(goal); 52 For0(i,len){ 53 int id = goal[i]-'a';cur = ch[cur][id]; 54 int now = cur; 55 while(now) { 56 ans += v[now];v[now] = 0; 57 now = next[now]; 58 } 59 } 60 } 61 }ac; 62 63 int main(){ 64 scanf("%d",&T); 65 For(i,T){ 66 scanf("%d",&n); 67 ac.clr(); 68 while(n--){ 69 scanf("%s",&goal); 70 ac.insert(goal); 71 } 72 ac.BuildNext(); 73 scanf("%s",&goal); 74 ac.match(); 75 printf("%d\n",ans);ans=0; 76 } 77 return 0; 78 }