hdu 2222 Keywords Search AC自动机模板题

本文介绍了一种基于AC自动机的字符串匹配算法,该算法能够高效地处理多个关键词的匹配问题,尤其适用于搜索引擎等场景。文章通过一个具体的编程实例详细展示了如何构建AC自动机,并实现文本中的关键词匹配。

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

 

这道题完全套用模板,但是注意的是有可能有多个相同的模式串,算总和的时候都要算上。

 

#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int ch[10002*52][26],End[10002*52],cur,fail[10002*52],last[10002*52],ans[10002];
char str[1000005],str0[10002][52];
int sum[10002*52];
void get_fail()
{
    int now,tmpFail,Next;
    queue<int> q;
    for(int j=0; j<26; j++)
    {
        if(ch[0][j])
        {
            q.push(ch[0][j]);
            fail[ch[0][j]] = 0;
            last[ch[0][j]] = 0;
        }
    }
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int j=0; j<26; j++)
        {
            if(!ch[now][j]) continue;
            Next = ch[now][j];
            q.push(Next);
            tmpFail = fail[now];
            while(tmpFail&&!ch[tmpFail][j]) tmpFail = fail[tmpFail];
            fail[Next] = ch[tmpFail][j];
            last[Next] = End[fail[Next]] ? fail[Next]:last[fail[Next]];
        }
    }
}
void Find ()
{
    int now = 0;
    int len = strlen(str);
    for(int i=0; i<len; i++)
    {
        if(str[i]<'a'||str[i]>'z')
        {
            now=0;
            continue;
        }
        str[i]-='a';
        while(now&&!ch[now][str[i]]) now = fail[now];
        now = ch[now][str[i]];
        if(End[now]) ans[End[now]]++;
        int tmp = now;
        while(last[tmp])
        {
            ans[End[last[tmp]]]++;
            tmp = last[tmp];
        }
    }
}
int main()
{
    int n,now,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(ch,0,sizeof(ch));
        memset(End,0,sizeof(End));
        memset(ans,0,sizeof(ans));
        memset(last,0,sizeof(last));
        memset(sum,0,sizeof(sum));
        cur = 1;
        int len;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",str0[i]);
            len = strlen(str0[i]);
            now = 0;
            for(int j=0; j<len; j++)
            {
                str0[i][j]-='a';
                if(ch[now][str0[i][j]]==0) ch[now][str0[i][j]] = cur++;
                now = ch[now][str0[i][j]];
                str0[i][j]+='a';
            }
            if(End[now]) sum[End[now]]++;
            else {
                sum[i] = 1;
                End[now] = i;
            }
        }
        get_fail();
        scanf("%s",str);
        Find();
        int res=0;
        for(int i=1; i<=n; i++)
        {
            if(ans[i]) res+=sum[i];
            //sum+=ans[i];
        }
        printf("%d\n",res);
    }
}

转载于:https://www.cnblogs.com/lastone/p/5341598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值