Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6306 Accepted Submission(s): 2134
Problem DescriptionIn 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.
InputFirst 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.
OutputPrint how many keywords are contained in the description.Sample Input
15shehesayshrheryasherhs
Sample Output
3/*題目大意:輸入資訊搜尋引擎在一大串圖片描述資訊中找與輸入資訊像匹配的的數目題目解答:字典樹+AC自動機AC自動機 http://blog.csdn.net/Hashmat/archive/2010/09/23/5902263.aspx字典樹 略Problem : 2222 ( Keywords Search ) Judge Status : AcceptedRunId : 2987974 Language : C++ Author : wawadimuCode Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta*/#include<iostream>#include<queue>using namespace std;#define kind 26#define maxn 1000010#define maxlen 51char res[maxlen];//字典串char str[maxn];//模式串struct trie{int cnt;trie* fail,*next[kind];trie(){cnt=0;fail=NULL;memset(next,NULL,sizeof(next));}}*q[500001];//空間開大一點void insert(char *res,trie *root);//插入單詞void ac_automation(trie* root);//ac自動機int query(char *str,trie* root);//尋找字典序中包含的單詞數目int main(){//freopen("2222.txt","r",stdin);int i,j,n;int cas;scanf("%d",&cas);while(cas--){trie *root=new trie();scanf("%d/n",&n);for(i=1;i<=n;i++) {scanf("%s/n",res);//printf("%s/n",res);insert(res,root);}scanf("%s/n",str);//printf("%s/n",str);ac_automation(root);int ans=query(str,root);printf("%d/n",ans);}return 0;}void insert(char *res,trie *root){trie *p=root;while(*res){//cout<<*res<<" ";int index=(*res++)-'a';if( p->next[index]==NULL )p->next[index]=new trie();p=p->next[index];}p->cnt++;}void ac_automation(trie* root){trie *tmp=NULL,*u=NULL,*p=root;p->fail=NULL;int front=0,rear=0;rear++;q[front]=p;while(front!=rear){u=q[front++];for(int i=0;i<kind;i++){if(u->next[i]!=NULL){if(u==root) u->next[i]->fail=root;else{tmp=u->fail;while(tmp!=NULL){if(tmp->next[i]!=NULL){u->next[i]->fail=tmp->next[i];tmp=tmp->next[i];break;}tmp=tmp->fail;}if(tmp==NULL) u->next[i]->fail=root;}q[rear++]=u->next[i];}}}}int query(char *str,trie *root){trie *tmp,*p=root;int cnt=0;while(*str){int index=(*str++)-'a';while(p!=root && p->next[index]==NULL) p=p->fail;p=p->next[index];if(p==NULL) p=root;else{tmp=p;while(tmp!=root && tmp->cnt!=-1)//避免重複增加同樣的單詞{cnt+=tmp->cnt;tmp->cnt=-1;tmp=tmp->fail;}}}return cnt;}