【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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值