HDU2222 Keywords Search

题目链接:HDU2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 49488    Accepted Submission(s): 15845


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自动机,先留个模板在这,之后慢慢深究更多的用法和变种。注意同一个单词可能出现多次,所以建树的时候应该是+1而不是赋值。

//
//  main.cpp
//  HDU2222
//
//  Created by teddywang on 16/4/5.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 500010
#define MIN 11000
using namespace std;
typedef struct node{
    int flag,num;
    node *next[26];
    node *fail;
}trienode;
trienode *root;
trienode *qu[MAX];
int ans,ptr,n,head,tail;
char str[2*MAX],dir[60];

trienode * creat_node()
{
    trienode *r;
    r=new node;
    r->fail=NULL;
    for(int i=0;i<26;i++)
        r->next[i]=NULL;
    r->flag=r->num=0;
    return r;
}


void init()
{
    ans=ptr=0;
    head=tail=0;
   root=creat_node();
}

void insert(char *s)
{
    trienode *h,*r=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(r->next[buf]==NULL)
        {
            h=creat_node();
            r->next[buf]=h;
            r=r->next[buf];
        }
        else r=r->next[buf];
    }
    r->num++;r->flag=1;
}

void build_ac()
{
    qu[head++]=root;
    while(tail<head)
    {
        trienode *p=qu[tail++];
        for(int i=0;i<26;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root) p->next[i]->fail=root;
                else{
                    p->next[i]->fail=p->fail->next[i];
                    if(p->next[i]->flag==0) p->next[i]->num=1;
                }
                qu[head++]=p->next[i];
            }
            else
            {
                if(p==root) p->next[i]=root;
                else p->next[i]=p->fail->next[i];
            }
        }
    }
}

int query(char *s)
{
    node *p=root;
    int num=0;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        p=p->next[buf];
        if(p->num)
        {
            trienode *temp=p;
            while(temp!=root)
            {
                if(temp->flag)
                {
                    num+=temp->num;
                }
                temp->num=0,temp=temp->fail;
            }
        }
    }
    return num;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        init();
        cin>>n;
        for(int i=0;i<n;i++)
        {
            scanf("%s",dir);
            insert(dir);
        }
        build_ac();
        scanf("%s",str);
        ans=query(str);
        cout<<ans<<endl;
    }
}


你可能感兴趣的:(AC自动机)