【AC自动机】Hdu2222:Keywords Search


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


Solution

裸的ac自动机
注意读懂题意:只要出现了ans+1,不管几次
坑点:字典会出现多个单词
第一次自己手打模版
好慢TAT 有待改进
然而感觉很爽

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;

#define maxn 10001

char s[10001][51],T[maxn*100];

int len[maxn*100],P[maxn*100],trie[maxn*100][26],cnt,poi,an,fail[maxn*100],temp,dog,ok[maxn*100];

void work(int dig)
{
    while(!trie[poi][dig]&&poi)poi=fail[poi];
    poi=trie[poi][dig];
    if(!poi)return;
    temp=poi;
    while(temp)
    {
        temp=fail[temp];
        if(ok[temp])an+=ok[temp],ok[temp]=0;
    }
    if(ok[poi])
    {
        an+=ok[poi],ok[poi]=0,temp=poi; 
    }
}

void add(int Poi,int dig)
{
    if(!trie[Poi][dig])
    {
        trie[Poi][dig]=++cnt;dog=cnt;
        while(1)
        {
            if(trie[fail[Poi]][dig]){fail[cnt]=trie[fail[Poi]][dig];break;}
            else if(!trie[fail[Poi]][dig]&&Poi==0)break;
            else Poi=fail[Poi];
        }
    }
    else dog=trie[Poi][dig];
}

int main()
{
    freopen("2222.in","r",stdin);
    freopen("2222.ans","w",stdout);
    int TT,L;
    scanf("%d",&TT);
    while(TT--)
    {
        cnt=0;an=0;
        int n,mx=0,p=0;
        memset(s,0,sizeof(s));
        memset(trie,0,sizeof(trie));
        memset(ok,0,sizeof(ok));
        memset(T,0,sizeof(T));
        memset(P,0,sizeof(P));
        memset(fail,0,sizeof(fail));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]),len[i]=strlen(s[i]),mx=max(mx,len[i]);
        for(int i=1;i<=n;i++)
        {
            if(!trie[0][s[i][0]-'a'])trie[0][s[i][0]-'a']=++cnt;
            P[i]=trie[0][s[i][0]-'a'];
            if(len[i]==1)ok[P[i]]++;
        }
        for(int i=1;i<mx;i++)
            for(int j=1;j<=n;j++)
            {
                if(i<len[j])add(P[j],s[j][i]-'a'),P[j]=dog;
                if(i==len[j]-1)ok[P[j]]++;
            }
        scanf("%s",T);
        L=strlen(T);
        for(int i=0;i<L;i++)
            work(T[i]-'a');
        printf("%d\n",an);
    }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信博6主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值