hdu 2222 Keywords Search
阿新 • • 發佈:2017-08-13
problem nts eof () 輸入 get end mis sizeof
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.
Output
Print how many keywords are contained in the description.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 66013 Accepted Submission(s): 22119
Wiskey also wants to bring this feature to his image retrieval system.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
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.
Sample Input 1 5 she he say shr her yasherhs
Sample Output 3
Author Wiskey
Recommend 題意:T組數據,每組數據輸入n ,輸入n個小字符串 ,最後一行輸入一個大字符串,輸出它包含多少個小字符串。 AC自動機練習(指針版) 屠龍寶刀點擊就送
#include <cstring> #include <cstdio> #define N 500010 struct node { int cnt; node * next[27],* fail; }*Q[N],*root; int T; node * create() { node * rt=new node; rt->cnt=0; rt->fail=0; memset(rt->next,0,sizeof(rt->next)); return rt; } void ins(char *a) { node * p=root; char *q=a; while(*q) { int id=*q-‘a‘+1; if(p->next[id]==NULL) p->next[id]=create(); p=p->next[id]; q++; } p->cnt++; } void build() { int head=0,tail=1; Q[tail]=root; while(head!=tail) { node * now=Q[++head]; node * tmp=NULL; for(int i=1;i<=26;i++) { if(now->next[i]!=NULL) { if(now==root) now->next[i]->fail=root; else { tmp=now->fail; while(tmp!=NULL) { if(tmp->next[i]!=NULL) { now->next[i]->fail=tmp->next[i]; break; } tmp=tmp->fail; } if(tmp==NULL) now->next[i]->fail=root; } Q[++tail]=now->next[i]; } } } } int query(char *a) { int ret=0; char *q=a; node *p=root; while(*q) { int id=*q-‘a‘+1; while(p->next[id]==NULL&&p!=root) p=p->fail; p=p->next[id]; if(p==NULL) p=root; node *tmp=p; while(tmp!=root&&tmp->cnt!=-1) { ret+=tmp->cnt; tmp->cnt=-1; tmp=tmp->fail; } ++q; } return ret; } int main() { scanf("%d",&T); for(int n;T--;) { root=create(); char str[55]; scanf("%d",&n); for(;n--;) { scanf("%s",str); ins(str); } build(); char key[1000005]; scanf("%s",key); printf("%d\n",query(key)); } return 0; }
hdu 2222 Keywords Search