hdu 3695 Computer Virus on Planet Pandora(AC自動機),hdu3695

來源:互聯網
上載者:User

hdu 3695 Computer Virus on Planet Pandora(AC自動機),hdu3695

題目串連:hdu 3695 Computer Virus on Planet Pandora

題目大意:給定一些病毒串,要求判斷說給定串中包含幾個病毒串,包括反轉。

解題思路:將給定的字串展開,然後匹配一次後反轉後再匹配一次。

#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <iostream>#include <algorithm>using namespace std;const int maxn = 250000;const int sigma_size = 26;struct Aho_Corasick {    int sz, g[maxn][sigma_size];    int tag[maxn], fail[maxn], last[maxn];    int vis[300], jump[300];    void init();    int idx(char ch);    void insert(char* str, int k);    void getFail();    void match(char* str);    void put(int u);    int solve(char* s);}AC;int N;char w[1005], s[5100005];int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        AC.init();        scanf("%d", &N);        for (int i = 1; i <= N; i++) {            scanf("%s", w);            AC.insert(w, i);        }        int mv = 0, x;        char ch;        getchar();        while(ch = getchar(), ch != '\n') {            if (ch == '[') {                scanf("%d%c", &x, &ch);                for (int i = 0; i < x; i++)                    s[mv++] = ch;                getchar();            } else                s[mv++] = ch;        }        s[mv] = '\0';        printf("%d\n", AC.solve(s));    }    return 0;}int Aho_Corasick::solve(char* s) {    memset(vis, 0, sizeof(vis));    AC.getFail();    AC.match(s);    reverse(s, s + strlen(s));    AC.match(s);    int ans = 0;    for (int i = 1; i <= N; i++)        if (vis[jump[i]])            ans++;    return ans;}void Aho_Corasick::init() {    sz = 1;    tag[0] = 0;    memset(g[0], 0, sizeof(g[0]));}int Aho_Corasick::idx(char ch) {    return ch - 'A';}void Aho_Corasick::put(int u) {    if (vis[tag[u]])        return;    vis[tag[u]] = 1;    if (last[u])        put(last[u]);}void Aho_Corasick::insert(char* str, int k) {    int u = 0, n = strlen(str);    for (int i = 0; i < n; i++) {        int v = idx(str[i]);        if (g[u][v] == 0) {            tag[sz] = 0;            memset(g[sz], 0, sizeof(g[sz]));            g[u][v] = sz++;        }        u = g[u][v];    }    if (tag[u] == 0)        tag[u] = k;    jump[k] = tag[u];}void Aho_Corasick::match(char* str) {    int n = strlen(str), u = 0;    for (int i = 0; i < n; i++) {        int v = idx(str[i]);        while (u && g[u][v] == 0)            u = fail[u];        u = g[u][v];        if (tag[u])            put(u);        else if (last[u])            put(last[u]);    }}void Aho_Corasick::getFail() {    queue<int> que;    for (int i  = 0; i < sigma_size; i++) {        int u = g[0][i];        if (u) {            fail[u] = last[u] = 0;            que.push(u);        }    }    while (!que.empty()) {        int r = que.front();        que.pop();        for (int i = 0; i < sigma_size; i++) {            int u = g[r][i];            if (u == 0) {                g[r][i] = g[fail[r]][i];                continue;            }            que.push(u);            int v = fail[r];            while (v && g[v][i] == 0)                v = fail[v];            fail[u] = g[v][i];            last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.