AC自动机指针版本和数组版本

本文介绍了一种使用AC自动机处理字符串匹配问题的方法,并通过具体的代码实现了字符串插入、查询及自动机构建过程。该方法适用于需要高效查找多个模式串的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 100003

struct Tr{
    int cnt;
    Tr *ne[26];
    Tr *fail;
    Tr() {
        cnt = 0;
        for (int i = 0;i < 26;i++) ne[i] = NULL;
        fail = NULL;
    }
    void init() {
        cnt = 0;
        for (int i = 0;i < 26;i++) ne[i] = NULL;
        fail = NULL;
    }
}root, tr[N];

int tot;

void Insert(char *s) {
    Tr *p = &root;
    int endId = 0;
    while (s[endId]) {
        int id = s[endId]-'a';
        if (p->ne[id] == NULL) {
            p->ne[id] = &tr[++tot];
            tr[tot].init();
        }
        p = p->ne[id];
        endId++;
    }
    p->cnt++;
}

int query(char *s) {
    int i, j, re = 0;
    Tr *p = &root;
    for (i = 0;s[i];i++) {
        int id = s[i]-'a';
        while (p->ne[id] == NULL && p != (&root)) p = p->fail;
        p = p->ne[id];
        if (p == NULL) p = &root;
        Tr *tmp = p;
        while (tmp != (&root) && tmp->cnt != -1) {
            re += tmp->cnt;
            tmp->cnt = -1;
            tmp = tmp->fail;
        }
    }
    return re;
}


char s[N], t[N];
Tr* Q[N];

void build() {
    Tr *p = &root;
    root.fail = NULL;
    int f = 0, r = 1;
    Q[f] = p;
    while (f < r) {
        p = Q[f++];
        for (int i = 0;i < 26;i++) {
            if (p->ne[i]) {
                if (p == (&root)) p->ne[i]->fail = &root;
                else {
                    Tr *tmp = p->fail;
                    while (tmp != NULL) {
                        if (tmp->ne[i]) {
                            p->ne[i]->fail = tmp->ne[i];
                            break;
                        }
                        tmp = tmp->fail;
                    }
                    if (tmp == NULL) p->ne[i]->fail = &root;
                }
                Q[r++] = p->ne[i];
            }
        }
    }
}

int main() {
    int T, n, i, j;
    scanf("%d", &T);
    while (T--) {
        tot = 0;
        root.init();
        scanf("%d", &n);
        getchar();
        int mxlen = 0;
        for (i = 0;i < n;i++) {
            gets(s);
            int len = strlen(s);
            if (mxlen < len) {
                mxlen = len;
                strcpy(t, s);
            }
            Insert(s);
        }
        build();
        if (query(t) == n) {
            puts(t);
        }else puts("No");
    }
}
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 100003

struct Tr{
    int tr[N][26], cnt[N], fail[N];
    int root, tot;
    int newTr() {
        int i, d = ++tot;
        for (i = 0;i < 26;i++) tr[d][i] = 0;
        fail[d] = cnt[d] = 0;
        return d;
    }
    void init() {
        tot = 0;
        root = newTr();
    }
    void Insert(char *s) {
        int d = root;
        for (int i = 0;s[i];i++) {
            int id = s[i]-'a';
            if (tr[d][id] == 0) tr[d][id] = newTr();
            d = tr[d][id];
        }
        cnt[d]++;
    }
    void build() {
        queue<int> Q;
        Q.push(root);
        while (!Q.empty()) {
            int d = Q.front();
            Q.pop();
            for (int i = 0;i < 26;i++) {
                if (tr[d][i]) {
                    if (d == root) {
                        fail[tr[d][i]] = root;
                    }else {
                        int tmp = fail[d];
                        while (tmp) {
                            if (tr[tmp][i]) {
                                fail[tr[d][i]] = tr[tmp][i];
                                break;
                            }
                            tmp = fail[tmp];
                        }
                        if (tmp == 0) fail[tr[d][i]] = root;
                    }
                    Q.push(tr[d][i]);
                }
            }
        }
    }
    int query(char *s) {
        int d = root, i, re = 0;
        for (i = 0;s[i];i++) {
            int id = s[i]-'a';
            while (d != root && tr[d][id] == 0) d = fail[d];
            d = tr[d][id];
            if (d == 0) d = root;
            int tmp = d;
            while (tmp != root && cnt[tmp] != -1) {
                re += cnt[tmp];
                cnt[tmp] = -1;
                tmp = fail[tmp];
            }
        }
        return re;
    }
}ans;

char s[N], t[N];

int main() {
    int T, n, i, j;
    scanf("%d", &T);
    while (T--) {
        ans.init();
        scanf("%d", &n);
        getchar();
        int mxlen = 0;
        for (i = 0;i < n;i++) {
            gets(s);
            int len = strlen(s);
            if (mxlen < len) {
                mxlen = len;
                strcpy(t, s);
            }
            ans.Insert(s);
        }
        ans.build();
        if (ans.query(t) == n) {
            puts(t);
        }else puts("No");
    }
}


D. Good Substrings time limit per test2 seconds memory limit per test512 megabytes You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr. The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q]. Input The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring. Output Print a single integer — the number of distinct good substrings of string s. Examples InputCopy ababab 01000000000000000000000000 1 OutputCopy 5 InputCopy acbacbacaa 00000000000000000000000000 2 OutputCopy 8 Note In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值