C + + Road set sail--ac Automaton (Keywords Search)

Source: Internet
Author: User

/*
Keywords Search

Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 47794 Accepted Submission (s): 15228


Problem Description
In 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.

Input
First 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.

Output
Print How many keywords is contained in the description.

Sample Input
1
5
She
He
Say
Shr
Her
Yasherhs

Sample Output
3
*/

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 6 using namespacestd;7 8 Chars[Wuyi],m[1000001];9 intT,n,sz,ans;Ten inta[500001][ -]/*Dictionary Tree*/, q[500001],point[500001],danger[500001]/*Word End Flag*/; One BOOLmark[500001]; A  - intins () - { the intnow=1, l=strlen (s); -  for(intI=0; i<l;i++) - { - intt=s[i]-'a'+1; + if(A[now][t]) now=A[now][t]; - Elsenow=a[now][t]=++sz; + }     Adanger[now]++;  at } -  - intAcmach () - { -q[0]=1;p oint[1]=0; - intnow,w=1, t=0; in  while(t<W) - { tonow=q[t++];  +  for(intI=1; i<= -; i++)     - {  the if(!a[now][i])Continue; * intk=Point[now]; $  while(!a[k][i]) k=Point[k];Panax Notoginsengpoint[a[now][i]]=A[k][i]; -q[w++]=A[now][i]; the } + } A } the  + intSolve () - { $ intk=1, l=strlen (m); $  for(intI=0; i<l;i++) - { -mark[k]=1; the intt=m[i]-'a'+1; -  while(!a[k][t]) k=Point[k];Wuyik=A[k][t]; the if(!Mark[k]) -  for(intj=k;j;j=Point[j]) Wu { -ans+=Danger[j]; Aboutdanger[j]=0; $ } - } -printf"%d\n", ans); - } A  + intMain () the { -scanf"%d",&T); $  while(t--) the { theans=0; thesz=1; thescanf"%d",&n); -  for(intI=1; i<= -; i++) a[0][i]=1; in  for(intI=0; i<n;i++) the { thescanf"%s", s); About ins (); the }  thescanf"%s", M);  the Acmach (); + solve (); -  for(intI=1; i<=sz;i++) the {Bayipoint[i]=danger[i]=mark[i]=0; the  for(intj=1; j<= -; j + +) thea[i][j]=0; - } - } the}

C + + Road set sail--ac Automaton (Keywords Search)

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.