HDU 2222 Keywords Search (Introduction to AC automata)

Source: Internet
Author: User

Keywords SearchTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 42138 Accepted Submission (s): 13289

Problem DescriptionIn The modern time, Search engine came into 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 Input
15shehesayshrheryasherhs

Sample Output
3

Test Instructions:give you a lot of pattern strings, and then give a female string, ask how many strings in the string inside. The pattern string may be the same, counting multiple.
Ideas:multi-mode string matching problem, AC automata entry question, AC automata is the classic application of BFS and DP, the code is very ingenious.
Code:
#include <cstdio> #include <cstring> #include <cstring> #include <algorithm> #include <queue >using namespace Std;const int Maxn=1000010;const int bsz=26;typedef long long Ll;char txt[1000010];bool Vis[510];int A  ns;struct trie{int CH[MAXN][BSZ],VAL[MAXN],SZ,CNT[MAXN]; CH Save trie val-node corresponding word cnt-node end word number int f[maxn],last[maxn];//f-mismatch pointer last-suffix link int newnode () {val[sz]=0; c        nt[sz]=0;        memset (ch[sz],-1,sizeof Ch[sz]);    return sz++;        } void Init () {sz=0;    NewNode ();    } int idx (char c)//Take the label of C specifically see the character why {return C ' a ';        } void Insert (char *st,int ID) {int u=0,n=strlen (ST), c,i;            for (i=0;i<n;i++) {c=idx (st[i]);            if (ch[u][c]==-1) Ch[u][c]=newnode ();        U=CH[U][C];        } Val[u]=id;    cnt[u]++;        } void Build () {int u=0,v,i;        Queue<int> Q;        f[0]=0;  for (i=0;i<bsz;i++)      {V=ch[u][i];            if (v==-1) ch[u][i]=0;                else {f[v]=0;            Q.push (v);            }} while (!q.empty ()) {U=q.front ();            Q.pop ();            Last[u]=val[f[u]]?f[u]:last[f[u]];                for (i=0;i<bsz;i++) {v=ch[u][i]; if (v==-1) ch[u][i]=ch[f[u]][i];                    Change NULL to make sense along the father mismatch pointer go to the first meaningful node else {f[v]=ch[f[u]][i];                Q.push (v);        }}}} bool Find (char *st,int m,int ID) {int n=strlen (ST), i,u=0,c,p,flag=0;  memset (vis,0,sizeof Vis);            You can mark which words appear in the same word and only mark one for (i=0;i<n;i++) {c=idx (st[i]);            U=CH[U][C];            P=val[u]?u:last[u];                while (p) {//vis[val[p]]=true;  if (Val[p]) {ans+=cnt[p]; cnt[p]=0;}            Plus the number of occurrences of the string 0 avoid repeated counting    flag=1;            P=LAST[P]; }} if (!flag) return false;//for (i=1;i<=m;i++)//if (Vis[i])//printf ("        %d ", i);//Putchar (' \ n ');    return true;    }} Ac;int Main () {int i,n,m,tot;    scanf ("%d", &tot);        while (tot--) {scanf ("%d", &n);        Ac.init ();            for (i=1;i<=n;i++) {scanf ("%s", txt); Ac.        Insert (Txt,i);        } ac.build ();        ans=0;        scanf ("%s", txt); Ac.        Find (Txt,n,i);    printf ("%d\n", ans); } return 0;} /*15shehesayherheryasherhs*/




HDU 2222 Keywords Search (Introduction to AC automata)

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.