现在的位置: 首页 > 算法 > 正文

poj 2222 Keywords Search,AC自动机模板题

2018年12月24日 算法 ⁄ 共 1553字 ⁄ 字号 评论关闭

HDU
2222 Keywords Search

题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L(L
<= 106)
目标串,求目标串出现了多少个模式串。


题解:AC自动机模板题,在Trie的每个结点设一个Count表示统计以这个结点结尾的模式串的个数(因为模式串可能有相同的),Insert的时候统计,Find的时候找到一个是模式串结尾结点的时候统计Count,并把这个结点的Count置0,表示已经统计过了。累加的Count即为答案。



#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

int ans;

const int SIGMA_SIZE = 26;
const int MAXNODE = 500000 + 100;

struct AhoCorasickAutomata {
    int ch[MAXNODE][SIGMA_SIZE];
    int f[MAXNODE];
    int val[MAXNODE];
    int last[MAXNODE];
    int sz;

    void init() {
        sz = 1;
        memset(ch[0], 0, sizeof ch[0] );
    }

    int idx(char c) {
        return c - 'a';
    }

    void insert(char *s, int v) {
        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]++;
    }

    void print(int j) {
        if(val[j]) {
            ans += val[j];
            val[j] = 0;
            print(last[j]);
        }
    }

    int 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 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) 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]];
            }
        }
    }
};

AhoCorasickAutomata ac;

char text[1000001], p[10001][51];
int n, T;

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        ac.init();
        for(int i=1; i<=n; ++i) {
            scanf("%s", p[i]);
            ac.insert(p[i], i);
        }

        ac.getFail();
        scanf("%s", text);

        ans = 0;
        ac.find(text);
        printf("%d\n", ans);

    }
    return 0;
}

抱歉!评论已关闭.