Keywords Search(hdu 2222)

题意:给出n个单词,一篇文章,询问有几个单词在文章中出现过。

/*
  AC自动机的裸题。
  题目标号牛的一比。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 500010
#define M 1000010
using namespace std;
int a[N][26],danger[N],point[N],mark[N],q[N],n,size;
char ch[M];
void insert(){
    int len=strlen(ch),now=1;
    for(int i=0;i<len;i++){
        int t=ch[i]-‘a‘;
        if(!a[now][t])a[now][t]=++size;
        now=a[now][t];
    }
    danger[now]++;
}
void acmach(){//构建自动机
    int h=0,t=1;
    q[1]=1;point[1]=0;
    while(h<t){
        int now=q[++h];
        for(int i=0;i<=25;i++){
            if(!a[now][i])continue;
            int k=point[now];
            while(!a[k][i])k=point[k];
            point[a[now][i]]=a[k][i];//类似于kmp的失败数组
            q[++t]=a[now][i];
        }
    }
}
void solve(){
    int len=strlen(ch),k=1,ans=0;
    for(int i=0;i<len;i++){
        mark[k]=1;
        int t=ch[i]-‘a‘;
        while(!a[k][t])k=point[k];
        k=a[k][t];
        if(mark[k])continue;
        for(int j=k;j;j=point[j]){
            ans+=danger[j];
            danger[j]=0;//每个单词只记录一次
        }
    }
    printf("%d\n",ans);
}
int main(){
    int T;scanf("%d",&T);
    while(T--){
        memset(a,0,sizeof(a));
        memset(point,0,sizeof(point));
        memset(danger,0,sizeof(danger));
        memset(mark,0,sizeof(mark));
        size=1;
        for(int i=0;i<=25;i++)a[0][i]=1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%s",ch);
            insert();
        }
        acmach();
        scanf("%s",ch);
        solve();
    }
    return 0;
}
时间: 2024-11-24 02:52:36

Keywords Search(hdu 2222)的相关文章

HDU 2222——Keywords Search(AC自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34020    Accepted Submission(s): 11009 Problem Description In the modern time, Search engine came into the life of everybody like

Keywords Search (ac 自动机)

Keywords Search  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 typ

HDOJ 题目2222 Keywords Search(AC自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 38611    Accepted Submission(s): 12437 Problem Description In the modern time, Search engine came into the life of everybody like

ACM学习历程—HDU2222 Keywords Search(字典树)

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

hdu----(2222)Keywords Search(ac自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35930    Accepted Submission(s): 11597 Problem Description In the modern time, Search engine came into the life of everybody like

HDU 2222 Keywords Search (AC自动机)

题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在这一位不匹配时转移到失配指针上.失配指针是转移到某个等于此位置最长后缀的位置,求法是bfs #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #in

HDU 2222 Keywords Search(AC自动机)

裸的AC自动机, 这倒题不能使用静态数组模拟建树的过程,10000*50*26这样会爆内存,所以使用指针,使用结构体动态分配new 这个可以用来做模板了 1 //#pragma comment(linker, "/STACK:1677721600") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmath>

[HDOJ2222]Keywords Search(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:求字典中所有模式串各出现了多少次. 建立AC自动机,查询的时候首先跳过所有cur->next[index]为NULL的情况,再跳过cur是root的情况.因为root不存任何信息. 过滤完上述情况,那么当前匹配串上的index位置必然在trie上出现过,那么就沿着当前点的fail一路找上去.因为trie某节点的fail指针要么指向root,要么指向与当前字符相同的节点.接着记下每个点的c

【HDU2222】Keywords Search(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