hdu2222 KeyWords Search AC自动机入门题

/**
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L(L <= 106)目标串,求目标串出现了多少个模式串。
思路:ac自动机入门题。。直接插入查询。
唯一需要特殊考虑的是存在多个相同的字符串;相同的字符串会在字典书上覆盖原先的。
解决方法1:用map<string,int>标记同一种字符串。之后利用标记来统计。
解决方法2:用num[i]标记字典树上某个节点为结尾的字符串出现次数。之后统计的时候,如果是第一次统计它,那么加上它,然后置为-1表示
下次不需要再统计它了。

AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/



///解法1:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = 22;
const int mod = 1e9+7;
const int maxnode = 50*10000+10;
const int sigma_size = 26;
int cnt[10005];
map<string,int> mp;
struct AhoCorasickAutomata
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    int f[maxnode];
    int last[maxnode];
    void clear(){sz = 1; memset(ch[0],0,sizeof ch[0]); }
    int idx(char c){return c-'a'; }

    void insert(char *s,int x)
    {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++){
            int c = idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz], 0, sizeof ch[sz]);
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = x;
    }

    void find(char *T){
        int n = strlen(T);
        int j = 0;
        for(int i = 0; i < n; i++){
            int c = idx(T[i]);
            //while(j&&!ch[j][c]) j = f[j];
            j = ch[j][c];
            if(val[j]) print(j);
            else if(last[j]) print(last[j]);
        }
    }

    void print(int j)
    {
        if(j){
            cnt[val[j]] = 1;
            print(last[j]);
        }
    }

    void getFail(){
        queue<int> q;
        f[0] = 0;
        for(int c = 0; c < sigma_size; c++){
            int u = ch[0][c];
            if(u){f[u] = 0; q.push(u); last[u] = 0;}
        }

        while(!q.empty()){
            int r = q.front(); q.pop();
            for(int c = 0; c < sigma_size; c++){
                int u = ch[r][c];
                if(!u){
                    ch[r][c] = ch[f[r]][c]; continue;
                }//if(!u) continue;
                q.push(u);
                int v = f[r];
                while(v&&!ch[v][c]) v = f[v];
                f[u] = ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }

} ac ;
char s[1000005];
char t[10005][55];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ac.clear();
        mp.clear();
        for(int i = 1; i <= n; i++){
            scanf("%s",t[i]);
            ac.insert(t[i],i);
            mp[string(t[i])] = i;///因为两个完全相同的字符串会覆盖原先的,所以用map标记属于同一个。这样可以都加到。
        }
        scanf("%s",s);
        ac.getFail();
        ms(cnt,0);
        ac.find(s);
        int ans = 0;
        for(int i = 1; i <= n; i++) ans += cnt[mp[string(t[i])]];
        printf("%d
",ans);
    }
    return 0;
}

/*
1
5
she
he
say
shr
her
yasherhs
*/



///解法2:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = 22;
const int mod = 1e9+7;
const int maxnode = 50*10000+10;
const int sigma_size = 26;
int cnt[10005];
map<string,int> mp;
int num[maxnode];///统计在自动机上到达i节点的这个字符串的相同字符串的个数。
struct AhoCorasickAutomata
{
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    int f[maxnode];
    int last[maxnode];
    void clear(){sz = 1; memset(ch[0],0,sizeof ch[0]); }
    int idx(char c){return c-'a'; }

    void insert(char *s,int x)
    {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++){
            int c = idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz], 0, sizeof ch[sz]);
                num[sz] = 0;
                val[sz] = 0;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        val[u] = x;
        num[u]++;
    }

    void find(char *T){
        int n = strlen(T);
        int j = 0;
        for(int i = 0; i < n; i++){
            int c = idx(T[i]);
            //while(j&&!ch[j][c]) j = f[j];
            j = ch[j][c];
            if(val[j]) print(j);
            else if(last[j]) print(last[j]);
        }
    }

    void print(int j)
    {
        if(j){
            if(num[j]!=-1){
                cnt[val[j]] = num[j];
                num[j] = -1;
            }
            print(last[j]);
        }
    }

    void getFail(){
        queue<int> q;
        f[0] = 0;
        for(int c = 0; c < sigma_size; c++){
            int u = ch[0][c];
            if(u){f[u] = 0; q.push(u); last[u] = 0;}
        }

        while(!q.empty()){
            int r = q.front(); q.pop();
            for(int c = 0; c < sigma_size; c++){
                int u = ch[r][c];
                if(!u){
                    ch[r][c] = ch[f[r]][c]; continue;
                }//if(!u) continue;
                q.push(u);
                int v = f[r];
                while(v&&!ch[v][c]) v = f[v];
                f[u] = ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }

} ac ;
char s[1000005];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ac.clear();
        mp.clear();
        for(int i = 1; i <= n; i++){
            scanf("%s",s);
            ac.insert(s,i);
        }
        scanf("%s",s);
        ac.getFail();
        ms(cnt,0);
        ac.find(s);
        int ans = 0;
        for(int i = 1; i <= n; i++) ans += cnt[i];
        printf("%d
",ans);
    }
    return 0;
}

/*
2
5
she
he
say
shr
her
yasherhs
2
ab
ab
aba
*/
原文地址:https://www.cnblogs.com/xiaochaoqun/p/7508895.html