Keywords Search hdu+AC自动机,eee其实和统计难题是一样的题

Keywords Search

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


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
 
解决方案:AC自动机的模板题
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct trie
{
    struct trie *child[26];///字典树的26个指针
    struct trie *fail;///字典树的fail指针
    int cnt;///标记防止出现相同单词
    trie()
    {
        for(int i=0; i<26; i++)
            child[i]=NULL;
        fail=NULL;
        cnt=0;
    }


};
trie *root;
int ans;
char word[55],text[1000004];
void create(char *word)
{
    trie *s=root;
    int i=0;
    while(word[i]!='\0')
    {
        int c=word[i]-'a';
        if(s->child[c]==NULL)
        {
            s->child[c]=new trie();///新的节点

        }
        s=s->child[c];
        i++;

    }
    s->cnt++;

}///建立动态字典树

void create_fail()
{
    queue<trie *>Q;
    Q.push(root);///先是根节点
    trie *s;
    while(!Q.empty())
    {
        s=Q.front();///根节点出队列
        Q.pop();
        for(int i=0; i<26; i++)
        {
            if(s->child[i]!=NULL)
            {
                if(s==root) s->child[i]->fail=root;///根节点的儿子的fail都指向根节点
                else
                {
                    trie *temp=s;
                    while(temp->fail!=NULL)
                    {
                        if(temp->fail->child[i]!=NULL)
                        {
                            s->child[i]->fail=temp->fail->child[i];
                            break;
                        }
                        temp=temp->fail;
                    }///沿其父节点fail指向的节点往上寻,有i,则该节点的fail指向它的儿子
                    if(temp->fail==NULL)
                    {
                        s->child[i]->fail=root;

                    }///若寻不到,则指向root
                }///若不是根节点,追随其父节点的fail指针找
                Q.push(s->child[i]);///寻到的加入队列

            }
        }


    }



}///构造fail指针
void AC_search()
{

    int i=0;
    trie *p=root;
    while(text[i])
    {
        //cout<<"sd";
        int id=text[i]-'a';
      //  cout<<text[i];
        while(p!=root&&p->child[id]==NULL)
        {
            p=p->fail;//cout<<"sd";
        }///查询失败,沿fail指针走
        if(p->child[id]!=NULL)
        {
            p=p->child[id];
            p=(p==NULL)?root:p;
            trie *temp=p;
            while(temp!=root&&temp->cnt!=0)
            {   //cout<<"sd";
                ans+=temp->cnt;
                temp->cnt=0;
                temp=temp->fail;
            }///记录出现过的单词
        }
        i++;
        //cout<<i<<endl;
    }


}
int main()
{
    int t;
    scanf("%d",&t);
    int N;
    while(t--)
    {
        root=new trie;
        scanf("%d",&N);
        for(int i=0; i<N; i++)
        {
            scanf("%s",word);
            create(word);
        }
        scanf("%s",text);
        ans=0;
        create_fail();
        AC_search();
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值