hdu2222 AC Automaton-several pattern strings appear in a given string

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2222



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 
/**hdu2222 AC Automaton-several pattern strings appear in a given string: AC Automaton Template title */#include <stdio.h> #include <string.h> #include <iostream > #include <string.h> #include <queue>using namespace std;struct trie{int next[500010][26],fail[500010]    , end[500010];    int root,l;        int NewNode () {for (int i=0; i<26; i++) {next[l][i]=-1;        } end[l++]=0;    return L-1;        } void Init () {l=0;    Root=newnode ();        } void Insert (char *buf) {int Len=strlen (BUF);        int now=root; for (int i=0; i<len; i++) {if (next[now][buf[i]-' a ']==-1) next[now][buf[i]-' a ']=newnode            ();        Now=next[now][buf[i]-' a '];    } end[now]++;        } void Build () {queue<int>q;        Fail[root]=root;            for (int i=0; i<26; i++) {if (next[root][i]==-1) next[root][i]=root; else {Fail[next[root][i]]=root;            Q.push (Next[root][i]); }} while (!            Q.empty ()) {int Now=q.front ();            Q.pop (); for (int i=0; i<26; i++) {if (next[now][i]==-1) Next[now][i]=next[fail[now]                ][i];                    else {fail[next[now][i]]=next[fail[now]][i];                Q.push (Next[now][i]);        }}}} int query (char *buf) {int Len=strlen (BUF);        int now=root;        int res=0;            for (int i=0; i<len; i++) {now=next[now][buf[i]-' a '];            int Temp=now;                while (Temp!=root) {res+=end[temp];                end[temp]=0;            TEMP=FAIL[TEMP];    }} return res;    }} Ac;char Buf[1000010];int main () {int T;    int n;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Ac.init (); For(int i = 0;i < n;i++)            {scanf ("%s", buf);        Ac.insert (BUF);        } ac.build ();        scanf ("%s", buf);    printf ("%d\n", Ac.query (BUF)); } return 0;}


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

hdu2222 AC Automaton-several pattern strings appear in a given string

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.