HDU 6208 AC自動機

題意:

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6208
給出n個字符串,要求找到一個字符串包含其他所有的字符串。


思路:

肯定是最長的那個字符串,如果存在兩個不一樣的額且長度都是最長的字符串,就肯定找不到。否則就用AC自動機跑一遍所有的串即可。


代碼:

#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 100005

struct trie {
    int next[maxn][26], fail[maxn], end[maxn];
    int root, cnt;
    int new_node () {
        memset (next[cnt], -1, sizeof next[cnt]);
        end[cnt++] = 0;
        return cnt-1;
    }
    void init () {
        cnt = 0;
        root = new_node ();
    }
    void insert (char *buf) {//字典樹插入一個單詞
        int len = strlen (buf);
        int now = root;
        for (int i = 0; i < len; i++) {
            int id = buf[i]-'a';
            if (next[now][id] == -1) {
                next[now][id] = new_node ();
            }
            now = next[now][id];
        }
        end[now]++;
    }
    void build () {//構建fail指針
        queue <int> q;
        fail[root] = root;
        for (int i = 0; i < 26; 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 < 26; 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;
        for (int i = 0; i < len; i++) {
            int id = buf[i]-'a';
            now = next[now][id];
            int tmp = now;
            while (tmp != root) {
                if (end[tmp]) {
                    res += end[tmp];
                    end[tmp] = 0;
                }
                tmp = fail[tmp];//沿着失配邊走
            }
        }
        return res;
    }
}ac;

int n;
char t[maxm], str[maxm];
string s[maxm];

int main () {
    //freopen("in.txt", "r", stdin);
    int T;
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d", &n);
        ac.init ();
        int maxlen = 0, id = -1;
        for (int i = 1; i <= n; i++) {
            scanf("%s", t);
            //cout << t << endl;
            s[i] = (string)t;
            ac.insert(t);
            int len = strlen(t);
            if (len > maxlen) {
                id = i;
                maxlen = len;
            }
        }
        bool flag = true;
        for (int i = 1; i <= n; i++) {
            if ((int)s[i].length() == maxlen) {
                if (s[i] != s[id]) {
                    flag = false;
                    break;
                }
            }
        }
        if (!flag) {
            puts("No");
            continue;
        }
        ac.build ();
        strcpy(str, s[id].c_str());
        int ans = ac.query(str);
        if (ans >= n) puts(str);
        else puts("No");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章