HDU2222 Keywords Search

来源:互联网 发布:成都多益网络原画招聘 编辑:程序博客网 时间:2024/05/16 19:52

传送门
Problem 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 should tell me how many keywords will be match.

Input
First line will contain one integer means how many cases will follow by.
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 be 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
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

Author
Wiskey

题目大意

给你多组数据,对于每组数据有多个字符串,求最终的字符串中出现过多少个之前的字符串。

题解

裸的AC自动机。(不会?戳这里 我就是看这个懂的)

CODE:

#include<cstdio>#include<cstring>struct queue{    int h,t;    int a[1000001];    void clear() {h=1,t=0;}    void push(int n) {a[++t]=n;}    int front() {return a[h];}    void pop() {h++;}    bool empty() {return h>t;}}q;struct AC{    int ch[26];    int fail,cnt;}a[500001];int n,m,tot;char s[1000001];inline void init(){    q.clear();    for(int i=0;i<=tot;i++)    {        memset(a[i].ch,0,sizeof(a[i].ch));        a[i].cnt=a[i].fail=0;    }    tot=0;    a[0].fail=-1;}inline void insert(char *s){    int now=0,p,len=strlen(s);    for(int i=0;i<len;i++)    {        p=s[i]-'a';        if(!a[now].ch[p]) a[now].ch[p]=++tot;        now=a[now].ch[p];    }    a[now].cnt++;}inline void makefail(){    q.push(0);    while(!q.empty())    {        int now=q.front();        q.pop();        for(int i=0;i<26;i++)          if(a[now].ch[i])          {            int p=a[now].fail;            while(p!=-1&&!a[p].ch[i]) p=a[p].fail;            if(p==-1) a[a[now].ch[i]].fail=0;            else a[a[now].ch[i]].fail=a[p].ch[i];            q.push(a[now].ch[i]);          }    }}inline int find(int now){    int ans=0;    while(now&&a[now].cnt!=-1)    {        ans+=a[now].cnt;        a[now].cnt=-1;        now=a[now].fail;    }    return ans;}inline int ask(char *s){    int ans=0,now=0,len=strlen(s);    for(int i=0;i<len;i++)    {        int p=s[i]-'a';        if(a[now].ch[p]) now=a[now].ch[p];        else        {            int pos=a[now].fail;            while(pos!=-1&&!a[pos].ch[p]) pos=a[pos].fail;            if(pos==-1) now=0;            else now=a[pos].ch[p];        }        ans+=find(now);    }    return ans;}int main(){    scanf("%d",&n);    while(n--)    {        scanf("%d",&m);        init();        for(int i=1;i<=m;i++)          scanf("%s",s),insert(s);        makefail();        scanf("%s",s);        printf("%d\n",ask(s));    }    return 0;}
0 0
原创粉丝点击