hdu 2222 Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 66013 Accepted Submission(s): 22119
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
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; }
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。