HDU 2896 病毒侵襲

Description
         當太陽的光輝逐漸被月亮遮蔽,世界失去了光明,大地迎來最黑暗的時刻。。。。在這樣的時刻,人們卻異常興奮——我們能在有生之年看到500年一遇的世界奇觀,那是多麼幸福的事兒啊~~
但網路上總有那麼些網站,開始藉着民衆的好奇心,打着介紹日食的旗號,大肆傳播病毒。小t不幸成爲受害者之一。小t如此生氣,他決定要把世界上所有帶病毒的網站都找出來。當然,誰都知道這是不可能的。小t卻執意要完成這不能的任務,他說:“子子孫孫無窮匱也!”(愚公後繼有人了)。
萬事開頭難,小t收集了好多病毒的特徵碼,又收集了一批詭異網站的源碼,他想知道這些網站中哪些是有病毒的,又是帶了怎樣的病毒呢?順便還想知道他到底收集了多少帶病毒的網站。這時候他卻不知道何從下手了。所以想請大家幫幫忙。小t又是個急性子哦,所以解決問題越快越好哦~~

Input 第一行,一個整數N(1<=N<=500),表示病毒特徵碼的個數。
接下來N行,每行表示一個病毒特徵碼,特徵碼字符串長度在20—200之間。
每個病毒都有一個編號,依此爲1—N。
不同編號的病毒特徵碼不會相同。
在這之後一行,有一個整數M(1<=M<=1000),表示網站數。
接下來M行,每行表示一個網站源碼,源碼字符串長度在7000—10000之間。
每個網站都有一個編號,依此爲1—M。
以上字符串中字符都是ASCII碼可見字符(不包括回車)。
Output         依次按如下格式輸出按網站編號從小到大輸出,帶病毒的網站編號和包含病毒編號,每行一個含毒網站信息。 web 網站編號: 病毒編號 病毒編號 …
冒號後有一個空格,病毒編號按從小到大排列,兩個病毒編號之間用一個空格隔開,如果一個網站包含病毒,病毒數不會超過3個。
最後一行輸出統計信息,如下格式
total: 帶病毒網站數
冒號後有一個空格。
Sample Input
3
aaa
bbb
ccc
2
aaabbbccc
bbaacc
Sample Output
web 1: 1 2 3
total: 1

首先祝一下我可愛的Neko 強生日快樂~ 永遠開心~

解題思路:

因爲這道題目要求要輸出在母串中出現過的字串的編號, 那麼就不能赤裸裸地上ac自動機的模板了, 原來的板子中end數組是記錄一個字符是否是一個子串的結尾並記錄以這個字符結尾的子串的個數, 那麼這道題我們把end裏記錄爲是第幾個子串即子串的編號即可。 因爲有多個母串, 所以我們每匹配一個母串就輸出一次答案, 如果沒有匹配的直接跳過, 最後輸出匹配母串總數。

不知道大家做ac自動機的題都是怎麼開數組大小的, 其實這道題不難, 但就因爲我開小了在vjudge上面一直顯示TLE???不是很懂找了好久才發現是數組開小, 很坑。

還有一個小細節要注意, 因爲題目描述裏寫 "以上字符串中字符都是ASCII碼可見字符(不包括回車)。" 所以next數組的第二維記得開大, 相應的初始化函數也要相應改大。

代碼:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*tools:
*ios::sync_with_stdio(false);
*freopen("input.txt", "r", stdin);
*/


typedef long long ll;
typedef unsigned long long ull;
const int dir[9][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0 };
const ll  ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int) 1e9 + 7;
const int Max = (int) 1e5 + 51;

bool ans[505];

struct Trie {
    int next[Max][129], fail[Max], end[Max];
    int root, L;
    int Newnode() {
        // initial L equal to 1
        for (int i = 0; i < 128; ++i) {
            next[L][i] = -1;
        }
        end[L++] = -1;
        return L - 1;
    }
    void Init() {
        L = 0;
        root = Newnode();
    }
    void Insert(char *buf, int num) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; ++i) {
            if (next[now][buf[i]] == -1)
                next[now][buf[i]] = Newnode();
            now = next[now][buf[i]];
        }
        end[now] = num;
    }
    void  Build() {
        queue < int> Q;
        fail[root] = root;
        for (int i = 0; i < 128; ++i) {
            if (next[root][i] == -1) {
                next[root][i] = root;
            }
            else {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        }
        while (!Q.empty()) {
            int now = Q.front();
            Q.pop();
            for (int i = 0; i < 128; ++i) {
                if (next[now][i] == -1) {
                    next[now][i] = next[fail[now]][i];
                }
                else {
                    fail[next[now][i]] = next[fail[now]][i];
                    Q.push(next[now][i]);
                }
            }
        }
    }
    int Query(char *buf) {
        int len = strlen(buf);
        int now = root;
        int res = 0;
        memset(ans, 0, sizeof(ans));
        for (int i = 0; i < len; ++i) {
            now = next[now][buf[i]];
            int temp = now;
            while (temp != root) {
                if (end[temp] != -1) {
                    ans[end[temp]] = 1;
                    res++;
                }
                temp = fail[temp];
            }
        }
        return res;
    }
    /*
    void Debug() {
        for (int i = 0; i < L; ++i) {
            printf("id = %3d, fail = %3d, end = %3d, chi = [", i, fail[i], end[i]);
            for (int j = 0; j < 26; ++j) {
                printf("%2d", next[i][j]);
            }
            printf("]\n");
        }
    }*/
};

char ch[10100];
Trie ac;

int main() {
    //freopen("input.txt", "r", stdin);

    // definition
    int n, m;

    // initialization
    scanf("%d", &n);
    ac.Init();

    for (int i = 0; i < n; ++i) {
        scanf("%s", ch);
        // record the id
        ac.Insert(ch, i + 1);
    }
    ac.Build();

    int tot = 0;
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        scanf("%s", ch);

        // reserve the id
        // algorithm in Query
        if (ac.Query(ch)) {
            tot++;
            printf("web %d:", i + 1);
            for (int i = 0; i < 505; ++i) {
                if (ans[i])
                    printf(" %d", i);
            } printf("\n");
        }
    }
    printf("total: %d\n", tot);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章