Hdu2222 (keywords search, AC automatic machine)

Source: Internet
Author: User

Portal

Keywords search
Time limit:1000 ms   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 shoshould tell me how many keywords will be match.

Input

First line will contain one integer means how many cases will follow.
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 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

15 shehesayshrheryasherhs

Sample output

3 .. It is also a bare question,... My TM has been handed over for 35 tttttttttttttttttt_____________ tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt... In the match function .. Int id = sT [I]-'A', cur = CH [cur] [ID]; and INT id = sT [I]-'A '; cur = CH [cur] [ID]; the difference between one character .. Bytes
 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 }

 

 

Hdu2222 (keywords search, AC automatic machine)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.