HDU2222(AC自动机)

Problem 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

题目的大体意思就是:有T组数据,给出n个单词作为字典,一个s字符串,查找s中出现多少个字典中的单词

分析:AC自动机必做题,这个自动机是自己搞的,O(∩_∩)O~呵呵
主要步骤分三步:
1.构建trie树
2.构建失配指针
3.像自动机一样奔跑

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>

using namespace std;

int ch[50*10010][30];
char w[10010][51];
char s[1000010];
int n,T,tot,fail[50*10010];
int iend[50*10010];
bool p[50*10010];

void build(int bh)
{
    int i,j,now=0;
    int len=strlen(w[bh]);
    for (i=0;i<len;i++)
    {
        int x=w[bh][i]-'a';
        if (!ch[now][x]) ch[now][x]=++tot;
        now=ch[now][x];
    }
    iend[now]++;
    return;
}

void make()
{
    int i,j;
    queue<int> q;
    for (i=0;i<26;i++)
       if (ch[0][i])
          q.push(ch[0][i]);
    while (!q.empty())
    {
        int r=q.front();
        q.pop();
        for (i=0;i<26;i++)
        {
            if (!ch[r][i])
            {
                ch[r][i]=ch[fail[r]][i];
                continue;
            }
            fail[ch[r][i]]=ch[fail[r]][i];
            q.push(ch[r][i]);
        }
    }
    return;
}

void solve()  //0是根节点 
{
    memset(p,1,sizeof(p));
    int len=strlen(s);
    int ans=0;
    int now=0,i;
    for (i=0;i<len;i++)
    {
        int x=s[i]-'a';
        while (!ch[now][x]&&now!=0) now=fail[now];  //他没有这个儿子,就找失配
        //找到根节点还没找到就停止了 
        now=ch[now][x];  //儿子 
        int f=now;
        while (f)  //回溯,直到根 
        {
            if (!p[f]) break;
            else if (p[f]&&iend[f]>0)  //从未访问过 
            {
                ans+=iend[f];
                p[f]=0;
            }
            f=fail[f];  //往失配上找,看看失配上有没有可以匹配的单词 
        }
    }   
    printf("%d\n",ans);
    return;
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        memset(iend,0,sizeof(iend));  //千万不要忘记初始化
        memset(ch,0,sizeof(ch));
        memset(fail,0,sizeof(fail));
        tot=0;
        memset(w,'\0',sizeof(w));
        memset(s,'\0',sizeof(s));
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%s",&w[i]);
            build(i);
        }
        make();
        scanf("%s",&s);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值