2017.10.16. AC自动机

AC自动机

适合题型:

1.多个子串与母串的匹配

样题: Keywords Search HDU - 2222

(以下是有道的机翻(比百度好多了))
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.
Wiskey还希望将这一功能引入他的图像检索系统。

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)
每个案例都包含两个整数,N表示关键字的数量和N个关键字。(N < = 10000)

Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
每个关键字只包含字符a-‘z’,长度将不超过50。

The last line is the description, and the length will be not longer than 1000000.
最后一行是描述,长度将不超过1000000。

Output
输出

Print how many keywords are contained in the description.
打印在描述中包含了多少关键字。

Sample Input
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

std.cpp:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

const int N = 10003;
const int M = 1e6+3;
const int L = 53;

int T,n,tot,vis[N*L];

struct node{
    int num,fail;
    int son[26];
    void init()
    {
        num = fail = 0;
        memset(son,0,sizeof(son));
    }
}trie[N*L];

char s[M];

void insert(char *s)
{
    int len = strlen(s);
    int u = 1;
    bool flag = false;
    for(int i=0;i<len;++i)
    {
        if(!trie[u].son[s[i]-'a'])
            trie[trie[u].son[s[i]-'a']=++tot].init();           
        u = trie[u].son[s[i]-'a'];
    }
    ++trie[u].num;
}

int qn,que[N*L];
void buildFail()
{
    que[qn=1]=1;
    for(int ql=1;ql<=qn;++ql)
    {
        int u = que[ql],v,w;
        for(int i=0;i<26;++i)
        {
            v = trie[u].fail;           
            while(!trie[v].son[i])v = trie[v].fail;             
            v=trie[v].son[i],w=trie[u].son[i];

            if(w)   trie[w].fail = v,que[++qn] = w;
            else    trie[u].son[i] = v;
        }
    }
}

int main()
{
    for(int i=0;i<26;++i)trie[0].son[i] = 1;

    scanf("%d",&T);
    for(int tt=1;tt<=T;++tt)
    {
        trie[tot=1].init();
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%s",s);
            insert(s);
        }
        buildFail();

        scanf("%s",s);
        int len = strlen(s);
        int now=1,tmp,ans=0;
        for(int i=0;i<len;++i)
        {
            now = trie[now].son[s[i]-'a'];
            tmp = now;
            while(tmp&&vis[tmp]!=tt)
            {
                vis[tmp] = tt;
                ans += trie[tmp].num;
                tmp = trie[tmp].fail;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

P.S

看不懂的可以到这位大佬的博客:
传送门:http://blog.csdn.net/creatorx/article/details/71100840

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值