HDU2222 Keywords Search

来源:互联网 发布:python api 中文 编辑:程序博客网 时间:2024/06/05 15:42

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32540    Accepted Submission(s): 10526



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
15shehesayshrheryasherhs
 

Sample Output
3
 

Author
Wiskey
 

AC自动机模板。代码有注释

#include<cstdio>#include<cstring>#include<queue>using namespace std;const int kind=26;struct node{    node *fail;    node *next[kind];    int count;    node()    {        fail=NULL;        count=0;        memset(next,0,sizeof(next));    }};queue<node *> q;char keyword[55];char s[1000010];void insert(char *str,node *root)       //建立字典树{    node *p=root;       //以root为根节点    int i=0,index;    while(str[i])    {        index=str[i]-'a';        if(p->next[index]==NULL)            p->next[index]=new node();  //root指向下一个节点,然后下一个节点再建立一个节点        p=p->next[index];   //p变成下一个节点        i++;    //字符串右移    }    p->count++; //字符串到底才有一个数字}   //字典树里的节点好像没有存放字符,字符使存放在字典树的边里,就是nextvoid build_ac_automation(node *root)    //建立ac自动机,弄出fail{    int i;    q.push(root);       //把根节点加入队列    root->fail=NULL;    while(!q.empty())   //队列非空    {        node *temp=q.front();        q.pop();        node *p=NULL;        for(i=0;i<26;i++)        {            if(temp->next[i]!=NULL) //如果temp还有子节点            {                if(temp==root)  //如果temp是根节点                    temp->next[i]->fail=root;   //那么根节点的子节点的fail都是指向根节点                else                {                    p=temp->fail;       //不是根节点,取出这个节点的fail                    while(p!=NULL)                    {                        if(p->next[i]!=NULL)    //不断的寻找p节点的子节点有没有和temp节点相同的元素                        {                            temp->next[i]->fail=p->next[i];     //相同的元素指向                            break;                        }                        p=p->fail;  //不断的指向fail知道为空,或者存在元素相同                    }                    if(p==NULL) //为空,就指向根节点                        temp->next[i]->fail=root;                }                q.push(temp->next[i]);  //字符存在边里            }        }    }}int query(node *root)       //查询操作{    int i=0,cnt=0,index,len=strlen(s);    node *p=root;   //从根节点开始    while(s[i])    {        index=s[i]-'a';        while(p->next[index]==NULL&&p!=root)    //字符不匹配,沿着失败路径指向的字符继续找            p=p->fail;        p=p->next[index];   //下一个字符        if(p==NULL) //如果为空了,返回根节点            p=root;        node *temp=p;        while(temp!=root&&temp->count!=-1)     //没走过,且不是根节点        {            cnt+=temp->count;            temp->count=-1;            temp=temp->fail;    //沿失败指针返回继续找        }        i++;    }    return cnt;}int main(){    int n,t,i;    scanf("%d",&t);    while(t--)    {        node *root=new node();        scanf("%d",&n);        getchar();        for(i=0;i<n;i++)        {            gets(keyword);            insert(keyword,root);        }        build_ac_automation(root);        scanf("%s",s);        printf("%d\n",query(root));    }    return 0;}


0 0
原创粉丝点击