problem DescriptionIn the modern time, Search engine came to the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring the feature to his image retrieval system.
Every image has a long description, when users type some keywords to find the image, the system would match the keywords W ITH 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 would b E match.
InputFirst line would contain one integer means how many cases would follow by.
Each case would contain integers n means the number of keywords and n keywords follow. (N <= 10000)
Each keyword would only contains characters ' a '-' Z ', and the length would be is not longer than 50.
The last line is the description, and the length would be a not longer than 1000000.
OutputPrint How many keywords is contained in the description.
Sample Input15shehesayshrheryasherhs
Sample Output3
"Test Instructions" gives you a lot of words, and then gives you an article that asks the number of times the word appears in the article. "Analysis" according to the input of the string built AC automata, constantly go fail accumulation can. Pay attention to understanding test instructions, the same string appears once only once, give several comparative pits of data:
45SHEHESAYSHRHERYASHERHS 5shehesayshrheryasherhs 3sheshesheshesheshe6shehehesayshrheryasherhs Answer: 3334
The code is as follows:
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7 using namespacestd;8 #defineMAXN 8001009 #defineMAXL 60Ten One CharS[MAXL]; A Charss[1000010]; - - structnode the { - intson[ -],cnt,fail; -}T[MAXN];inttot; - + voidUpdintx) - { +T[x].cnt=0; Amemset (T[x].son,0,sizeof(T[x].son)); at } - - voidRead_trie () - { - intN; -scanf"%d",&n); intot=0; UPD (0); - for(intI=1; i<=n;i++) to { + intNow ; -scanf"%s", s+1); the intLen=strlen (s+1); *now=0; $ for(intj=1; j<=len;j++)Panax Notoginseng { - intind=s[j]-'a'+1; the if(!T[now].son[ind]) +t[now].son[ind]=++tot,upd (tot); Anow=T[now].son[ind]; the if(J==len) t[now].cnt++; + } - } $ } $ -queue<int>Q; - voidBuild_ac () the { - inti,j,x,y;Wuyi while(!q.empty ()) Q.pop (); theQ.push (0); - while(!q.empty ()) Wu { -x=Q.front (); Abouty=T[x].fail; $ for(j=1; j<= -; j + +) - { - if(T[x].son[j]) - { A Q.push (T[x].son[j]); +T[t[x].son[j]].fail=x?t[y].son[j]:x; the } - Elset[x].son[j]=T[y].son[j]; $ } the Q.pop (); the } the } the - intGmark (intXinty) in { the if(!x)returny; the if(t[x].cnt) y+=t[x].cnt,t[x].cnt=0; About returnGmark (t[x].fail,y); the } the the voidFfind () + { -scanf"%s", ss+1); the intL=strlen (ss+1);Bayi intnow,ans=0; the for(intI=1; i<=l;i++) the { - intx=ss[i]-'a'+1; -now=T[now].son[x]; theAns+=gmark (now,0); the } theprintf"%d\n", ans); the } - the intMain () the { the intT;94scanf"%d",&T); the while(t--) the { the Read_trie ();98 Build_ac (); About Ffind (); - }101 return 0;102}
[HDU2222]
2016-06-14 17:02:09
"HDU2222" Keywords Search (AC automaton)