码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2222 AC自动机

时间:2017-02-26 22:35:53      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:length   java   set   case   std   accept   front   when   ast   

Keywords Search

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


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
 

 题意:

给出n个模板串和一个文本串,问文本串中出现了多少个模板串。

代码:

//模板题。字典树+KMP=AC自动机。将所有的模板串用字典树建树,然后类似KMP处理。
//本题加一个vis 数组标记模板串是否存在。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxnod=1000006;
const int sigma_size=26;
const int maxn=1000006;
int nod[maxnod][sigma_size],f[maxnod],val[maxnod],last[maxnod],vis[maxnod];
int sz,ans;
void init(){
    sz=1;
    memset(nod[0],0,sizeof(nod[0]));
    val[0]=0;
    vis[0]=f[0]=last[0]=0;
}
void insert(char *s){
    int len=strlen(s);
    int rt=0;
    for(int i=0;i<len;i++){
        int id=s[i]-a;
        if(nod[rt][id]==0){
            vis[sz]=0;
            memset(nod[sz],0,sizeof(nod[sz]));
            nod[rt][id]=sz;
            val[sz++]=0;
        }
        rt=nod[rt][id];
    }
    val[rt]++;
    vis[rt]=1;
}
void getfail(){
    queue<int>q;
    f[0]=0;
    for(int i=0;i<sigma_size;i++){
        int u=nod[0][i];
        if(u) {f[u]=0;q.push(u);last[u]=0;}
    }
    while(!q.empty()){
        int r=q.front();q.pop();
        for(int i=0;i<sigma_size;i++){
            int u=nod[r][i];
            if(!u) continue;
            q.push(u);
            int v=f[r];
            while(v&&!nod[v][i]) v=f[v];
            f[u]=nod[v][i];
            last[u]=(val[f[u]]?f[u]:last[f[u]]);
        }
    }
}
void print(int i){
    if(i){
        if(vis[i]) {ans+=val[i];vis[i]=0;}
        print(last[i]);
    }
}
void find(char *t){
    int len=strlen(t);
    int j=0;
    for(int i=0;i<len;i++){
        int id=t[i]-a;
        while(j&&!nod[j][id]) j=f[j];
        j=nod[j][id];
        if(val[j]) print(j);
        else if(last[j]) print(last[j]);
    }
}
int main()
{
    int n,cas;
    char s[60],t[maxn];
    scanf("%d",&cas);
    while(cas--){
        ans=0;
        init();
        scanf("%d",&n);
        while(n--){
            scanf("%s",s);
            insert(s);
        }
        getfail();
        scanf("%s",t);
        find(t);
        printf("%d\n",ans);
    }
    return 0;
}

 

HDU2222 AC自动机

标签:length   java   set   case   std   accept   front   when   ast   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6446165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!