AC自动机MLE求条

P3796 AC 自动机(简单版 II)

@[___PatrickChen___](/user/608273) s[N] 太大了。 改成开一个读一个。
by Lu_xZ @ 2024-10-09 14:41:29


@[Lu_xZ](/user/963559) 不可行 <https://www.luogu.com.cn/record/181140034>
by ___PatrickChen___ @ 2024-10-09 21:44:34


$N$ 开大了,开 $10^5$ 就行 @[___PatrickChen___](/user/608273)
by bladrrxy @ 2024-10-19 17:31:59


@[bladrrxy](/user/939115) [不行](https://www.luogu.com.cn/record/183322369)
by ___PatrickChen___ @ 2024-10-19 19:45:54


@[___PatrickChen___](/user/608273) ``` #include <bits/stdc++.h> #define endl '\n' using namespace std; using ll = long long; const ll N = 2e4 + 5; int n, ch[N][30], flag[N], fail[N], idx, mx; string s[N]; struct node { int cnt, id; } ans[N]; int getnum(char c) { return c - 'a'; } void insert(string s, int id) { int p = 0, len = s.size(); for (int i = 0; i < len; ++i) { int c = getnum(s[i]); if (!ch[p][c]) ch[p][c] = ++idx; p = ch[p][c]; } flag[p] = id; } void get_fail() { queue<int>q; for (int i = 0; i < 26; ++i) { if (ch[0][i]) { int u = ch[0][i]; q.push(u); } } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++i) { if (ch[u][i]) { fail[ch[u][i]] = ch[fail[u]][i]; q.push(ch[u][i]); } else ch[u][i] = ch[fail[u]][i]; } } } void query(string s) { int p = 0, len = s.size(); for (int i = 0; i < len; ++i) { int c = getnum(s[i]); p = ch[p][c]; for (int j = p; j; j = fail[j]) ++ans[flag[j]].cnt; } } void init() { for (int i = 0; i < N; ++i) { memset(ch[i], 0, sizeof ch[i]); fail[i] = 0; flag[i] = 0; } } void solve() { init(); idx=0; for (int i = 1; i <= n; ++i) { cin >> s[i]; insert(s[i], i); ans[i].cnt = 0; ans[i].id = i; } get_fail(); cin >> s[0]; query(s[0]); sort(ans + 1, ans + n + 1, [](node x, node y) { return x.cnt > y.cnt; }); cout << (mx = ans[1].cnt) << endl; sort(ans + 1, ans + n + 1, [](node x, node y) { return x.id < y.id; }); for (int i = 1; i <= n; ++i) { if (ans[i].cnt == mx) cout << s[ans[i].id] << endl; } cin >> n; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; while (n) solve(); return 0; } ``` 这样就行,自己看改哪了。
by bladrrxy @ 2024-10-19 20:14:51


@[bladrrxy](/user/939115) [还是过不了](https://www.luogu.com.cn/record/183333745)
by ___PatrickChen___ @ 2024-10-19 20:16:39


@[___PatrickChen___](/user/608273) 你没改全
by bladrrxy @ 2024-10-19 20:18:53


init和solve前几句
by bladrrxy @ 2024-10-19 20:19:37


@[bladrrxy](/user/939115) thx
by ___PatrickChen___ @ 2024-10-19 20:41:58


|