HDU

来源:互联网 发布:淘宝怎样设置自动回复 编辑:程序博客网 时间:2024/06/18 07:54

Keywords Search



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
 



题意:多模匹配


解题思路:AC自动机……保存一下我自己的模板~~~关于AC自动机的学习,参考  http://blog.csdn.net/creatorx/article/details/71100840真是太牛逼了……Trie+KMP=AC自动机



#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;typedef long long int ll;struct node{    node *next[26];    node *fail;    int sum;};node* newnode(){    node *newnode=new node;    for(int j=0;j<26;j++)        newnode->next[j]=0;    newnode->sum=0;    newnode->fail=0;}void insert(node* root,char s[],int len){    node *p=root;    for(int i=0;i<len;i++){        int x=s[i]-'a';        if(p->next[x]==0){            p->next[x]=newnode();        }        p=p->next[x];    }    p->sum++;}node* que[1000005];//手写队列,快那么几十毫秒void build_fail(node *root){    int head=0;    int tail=1;    node *p;    node *temp;    que[head]=root;    while(head<tail){        temp=que[head++];        for(int i=0;i<26;i++){            if(temp->next[i]){                if(temp==root)                    temp->next[i]->fail=root;                else{                    p=temp->fail;                    while(p){                        if(p->next[i]){                            temp->next[i]->fail=p->next[i];                            break;                        }                        p=p->fail;                    }                    if(p==0)                        temp->next[i]->fail=root;                }                que[tail++]=temp->next[i];            }            else                temp->next[i]=temp!=root?temp->fail->next[i]:root;//优化?        }    }}int AC_Match(char s[],int len,node *root){    node *p=root;    node *temp;    int ans=0;    for(int i=0;i<len;i++){        int x=s[i]-'a';        while(!p->next[x]&&p!=root)            p=p->fail;        p=p->next[x];        if(p==0)            p=root;        temp=p;        while(temp!=root){            if(temp->sum>=0){                ans+=temp->sum;                temp->sum=-1;            }            else                break;            temp=temp->fail;        }    }    return ans;}char a[1000005];char b[100];node *root;int main(){    int t;    scanf("%d",&t);    while(t--){        int n;        scanf("%d",&n);        root=newnode();        while(n--){            scanf("%s",b);            insert(root,b,strlen(b));        }        scanf("%s",a);        build_fail(root);        printf("%d\n",AC_Match(a,strlen(a),root));    }    return 0;}


原创粉丝点击