HDU 2222 Keywords Search

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.

题意:
第一行给出测试的组数t
第二行给出需要插入的字符串数n
下面n行,每行一个字符串
最后一行一个字符串str。
问一共有几个字符串出现在str中。

思路:AC自动机模板题(wa了6次)
坑点,val【root】在遍历以后记得清0就好了。因为我们在找到一个字符串之后还会顺着失配边回去看看还有没有其他的字符串,这样就多计数了。
AC代码:

#include<bits/stdc++.h>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define MAXN 1000000+10
#define ssize  200010
#define SIGMA_SIZE 27
#define mod 100000
using namespace std;
typedef long long LL;

int trie[500000+10][SIGMA_SIZE];
int f[500000+10];
LL val[500000+10];
int last[500000+10];
char str[MAXN];
int tot,n,t;LL sum;

void init()
{
    tot=1;sum=0;
    memset(f,0,sizeof(f));
    memset(trie,0,sizeof(trie));
    memset(val,0,sizeof(val));
    memset(last,0,sizeof(last));
}

void Insert(char *str,int v)
{
    int root=0;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'a';
        int u=trie[root][id];
        if(!u)
        {
            memset(trie[tot],0,sizeof(trie[tot]));
            val[tot]=0;
            trie[root][id]=tot++;
        }
        root=trie[root][id];
    }
    val[root]+=1;
}

void getfail()
{
    queue<int>q;
    f[0]=0;
    for(int i=0;i<SIGMA_SIZE;i++)
    {
        int u=trie[0][i];
        if(u)
        {
            q.push(u);f[u]=0;last[u]=0;
        }
    }
    while(!q.empty())
    {
        int r=q.front();q.pop();
        for(int c=0;c<SIGMA_SIZE;c++)
        {
            int u=trie[r][c];
            if(!u)
            {
                trie[r][c]=trie[f[r]][c];
                continue;
            }
            q.push(u);int v=f[r];
            f[u]=trie[v][c];
            last[u]=val[f[u]]?f[u]:last[f[u]];
        }
    }
}

void cal(int root)
{
    if(root)
    {
        sum+=val[root];
        val[root]=0;
        cal(last[root]);
    }
}

void query(char *str)
{
    int root=0;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'a';
        while(root&&!trie[root][id])
        {
            root=f[root];
        }
        root=trie[root][id];
        if(val[root])
            cal(root);
        else if(last[root])
            cal(last[root]);
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            Insert(str,1);
        }
        getfail();
        scanf("%s",str);
        query(str);
        printf("%lld\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值