HDU3065(AC自动机入门题)

病毒侵袭持续中

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9831    Accepted Submission(s): 3457


Problem Description
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
 
Input
第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
 
Output
按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
 
Sample Input
3
AA
BB
CC
ooxxCC%dAAAoen....END
 
Sample Output
AA: 2
CC: 1
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=500005;
const int N=128;
int n;
int Hash[1005];
struct ID{
    char ss[55];
    ID()
    {
        memset(ss,0,sizeof(ss));
    }
}words[1005];
struct Trie{
    int next[MAXN][N],fail[MAXN],end[MAXN];
    int tot,root,id;
    int newnode()
    {
        for(int i=0;i<N;i++)
            next[tot][i]=-1;
        end[tot++]=0;
        return tot-1;    
    }
    
    void init()
    {
        tot=0;
        root=newnode();
    }
    
    void insert(char buf[],int id)
    {
        int len=strlen(buf);
        int now=root;
        for(int i=0;i<len;i++)
        {
            int k=buf[i];
            if(next[now][k]==-1)    
                next[now][k]=newnode();
            now=next[now][k];
        }
        end[now]=id;
    }
    
    void build()
    {
        fail[root]=root;
        queue<int> que;
        for(int i=0;i<N;i++)
            if(next[root][i]==-1)
                next[root][i]=root;
            else
            {
                fail[next[root][i]]=root;
                que.push(next[root][i]);
            }
        
        while(!que.empty())
        {
            int now=que.front();
            que.pop();
            
            for(int i=0;i<N;i++)
                if(next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    que.push(next[now][i]);    
                }
        }    
    }
    
    void query(char buf[])
    {
        int len=strlen(buf);
        int now=root;
        for(int i=0;i<len;i++)
        {
            now=next[now][buf[i]];
            int temp=now;
            while(temp!=root)
            {
                if(end[temp]!=0)
                    Hash[end[temp]]++;
                //若每个模式串只在主串中匹配一次则加上 end[temp]=0; 
                temp=fail[temp];
            }
        }
        
        for(int i=1;i<=n;i++)
        {
            if(Hash[i]!=0)
            {
                printf("%s: %d
",words[i].ss,Hash[i]);
            }
        }
    }
};
Trie ac;
char buf[2000005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(Hash,0,sizeof(Hash));
        ac.init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",words[i].ss);
            ac.insert(words[i].ss,i);
        }
        ac.build();
        scanf("%s",buf);
        ac.query(buf);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/program-ccc/p/5210694.html