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\n",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\n",ans);
    }
    return 0;
}

/*
2
5
she
he
say
shr
her
yasherhs
2
ab
ab
aba
*/
相關文章
相關標籤/搜索