HDU-Keywords Search(AC自動機)

題目鏈接:

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

Keywords Search

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

題意描述:

給你n個單詞和一篇文章,找出這n個單詞在文章中共出現多少次

程序代碼:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;

struct node{
    node *fail;
    node *next[26];
    int countn;
};
node *root;
char str[55];
char s[1000005];

void GetTrie(char *str);
void BuildAcAutomation();
int query();

int main()
{
    int len,i,T,n;
    scanf("%d",&T);
    while(T--)
    {
        root=new node();
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",str);
            GetTrie(str);
        }
        scanf("%s",s);
        BuildAcAutomation();
        printf("%d\n",query());
    }
    return 0;
}
void GetTrie(char *str)
{
    node *p;
    int i,id;
    p=root;
    for(i=0;str[i]!='\0';i++)
    {
        id=str[i]-'a';
        if(p->next[id]==NULL)
            p->next[id]=new node();
        p=p->next[id];
    }
    p->countn++;//代表單詞數加1
    //printf("***%d\n",p->countn);
}
void BuildAcAutomation()
{
    int i;
    node *temp,*p;
    p=new node();
    queue<node*>q;
    q.push(root);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        for(i=0;i<26;i++)
        {
            if(temp->next[i]!=NULL)
            {
                if(temp==root)
                    temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {
                        if(p->next[i]!=NULL)
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)
                        temp->next[i]->fail=root;
                }
                q.push(temp->next[i]);
            }
        }
    }
}
int query()
{
    int i,id,sum=0;
    node *p,*temp;
    p=root;
    for(i=0;s[i]!='\0';i++)
    {
        id=s[i]-'a';
        while(p->next[id]==NULL&&p!=root)
            p=p->fail;
        p=p->next[id];
        if(p==NULL)
            p=root;
        temp=p;
        while(temp!=root&&temp->countn!=0)
        {
            sum+=temp->countn;
            temp->countn=0;
            temp=temp->fail;
        }
    }
    return sum;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章