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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值