HDU 2222 Keywords Search         ( 模板题 AC自动机:模式串中有多个相同的串)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 81240    Accepted Submission(s): 28348


 

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

 

 

Author

Wiskey

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 

 

题意:给定N(N <= 10000)个长度不大于50的模式串,(可以算出maxnode=5000000,竟然给我T),再给定一个长度为L(L <= 106)目标串,求目标串出现了多少个模式串。

题解:AC自动机模板题:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

在每个trie结点存储一个val值和vis,每次插入一个单词的时候对单词结尾结点的val值进行自增,vis标记为1(不能将val值直接置为1,因为有可能模式串中有多个相同的串,它们是要被算作多次的),然后在询问的时候,每次计数完毕之后,将vis值标为0表示它已经被计算过了。最后输出所有val的累加和即可。

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
 
const int maxnode=510000;
const int sigma_size=26;
int ans=0;
struct AC_Automata
{
    int ch[maxnode][sigma_size];
    int val[maxnode];   // 以单词结尾的个数
    int f[maxnode];     // fail函数
    int last[maxnode];  // last[i]=j表j节点表示的单词是i节点单词的后缀,且j节点是单词节点
    int sz;
    bool vis[maxnode];//非单词节点vis=0,单词节点vis=1.如果用find找到了单词i节点,那么vis=0.
 
    //初始化0号根节点的相关信息
    void init()
    {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        last[0]=f[0]=0;
        vis[0]=0;
    }
 
    //insert负责构造ch与val数组
    //插入字符串,v必须非0表示一个单词节点
    void insert(char *s)
    {
        int n=strlen(s),u=0;
        for(int i=0; i<n; i++)
        {
            int id=s[i]-'a';
            if(ch[u][id]==0)
            {
                ch[u][id]=sz;
                 vis[sz]=0;
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz++]=0;
            }
            u=ch[u][id];
        }
        vis[u]=1;
        val[u]++;
    }
 
    //getFail函数负责构造f和last数组
    void getFail()
    {
        queue<int>q;
        last[0]=f[0]=0;
        for(int i=0; i<sigma_size; i++)
        {
            int u=ch[0][i];
            if(u)
            {
                f[u]=last[u]=0;
                q.push(u);
            }
        }
 
        while(!q.empty())// 按BFS顺序计算fail
        {
            int r=q.front(); q.pop();
            for(int i=0; i<sigma_size; i++)
            {
                int u=ch[r][i];
                if(u==0)continue;
                q.push(u);
 
                int v=f[r];
                while(v && ch[v][i]==0) v=f[v];
                f[u]= ch[v][i];
                last[u]=val[f[u]]?f[u]:last[f[u]];
            }
        }
    }
 
    //递归打印与结点i后缀相同的前缀节点编号
    //进入此函数前需保证val[i]>0
    void print(int i)
    {
        if(val[i])
        {
        	if(vis[i])
            {
            ans+=val[i];
            vis[i]=0;
            }
            print(last[i]);
        }
    }
 
    // 在s中找出 出现了哪几个模板单词
    void find(char *s)
    {
        int n=strlen(s),j=0;
        for(int i=0; i<n; i++)
        {
            int id=s[i]-'a';
            while(j && ch[j][id]==0) j=f[j];
            j=ch[j][id];
            if(val[j]) print(j);
            else if(last[j]) print(last[j]);
        }
    }
 
};
AC_Automata ac;
 
char word[80];
char txt[1000000+1000];
int main()
{
    int n;
    int T;
    scanf("%d",&T);
    while(T--)
    {
    	scanf("%d",&n);
        ac.init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",word);
            ac.insert(word);
        }
        ans=0;
        ac.getFail();
        scanf("%s",txt);
        ac.find(txt);
        printf("%d\n",ans);
        
    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值