HDU 2222 Keywords Search[自动机]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

题目大意:给n个模板串,求长串中模板串的个数;

解题思路:

自动机入门题,注意模板重复串。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=100007;

/******************auther:ACsorry******************/

struct Trie{
    Trie *next[26];
    Trie *fail;
    int flag;
    Trie(){
        mem(next,0);
        fail=0;
        flag=0;
    }
};

int tot;
Trie *p[500007];

Trie* newTrie()
{
    p[tot]=new Trie();
    tot++;
    return p[tot-1];
}

void sInsert(Trie *root,char *s)
{
    int i=0;
    Trie *p=root;
    while(s[i]){
        int idx=s[i]-'a';
        if(p->next[idx]==NULL){
            p->next[idx]=newTrie();
        }
        p=p->next[idx];
        i++;
    }
    p->flag++;
}

void buildAc(Trie *root)
{
    queue<Trie*> que;
    que.push(root);
    while(!que.empty()){
        Trie *cur=que.front();
        que.pop();
        for(int i=0;i<26;i++){
            if(cur->next[i]){
                if(cur==root) cur->next[i]->fail=root;  //第一个元素匹配
                else{
                    Trie *p=cur->fail;
                    while(p){  //搜索匹配
                        if(p->next[i]){
                            cur->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL) cur->next[i]->fail=root; //匹配为空,从头开始
                }
                que.push(cur->next[i]);
            }
        }
    }
}

int solve(Trie *root,char *s)
{
    int ret=0;
    int len=strlen(s);
    Trie *p=root;
    for(int i=0;i<len;i++){
        int idx=s[i]-'a';
        while(p->next[idx]==NULL&&p!=root) p=p->fail;
        p=p->next[idx];
        if(p==NULL) p=root;
        Trie *tmp=p; //ret+=(以p为后缀的串)
        while(tmp!=root&&tmp->flag!=-1){
            ret+=tmp->flag;
            tmp->flag=-1;
            tmp=tmp->fail;
        }
    }
    return ret;
}

char s[1000007];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        Trie *root=newTrie();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            sInsert(root,s);
        }
        buildAc(root);
        scanf("%s",s);
        printf("%d\n",solve(root,s));
        for(int i=0;i<tot;i++) delete p[i];
    }
    return 0;
}
时间: 2024-11-08 19:19:33

HDU 2222 Keywords Search[自动机]的相关文章

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

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

HDU 2222 - Keywords Search

试个模板- - /* HDU 2222 - Keywords Search [ AC自动机 ] */ #include <bits/stdc++.h> using namespace std; const int N = 500005; const int SIZE = 26; struct Trie { int ch[N][SIZE]; int f[N], last[N], cnt[N], val[N]; int tot, ans; void init() { tot = 0; memset

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

HDU 2222 Keywords Search (AC自动机模板题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu 2222 Keywords Search(AC自动机入门)

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

HDU 2222 Keywords Search(瞎搞)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 50451    Accepted Submission(s): 16236 Problem Description In the modern time, Search engine came into the life of everybody lik