P14363 谁没判呢?

· · 题解

没保证 |t_1| = |t_2|,谁没判呢?/ll

纯 trie,可以线性的做法,但是考场上我写的是 O(n + q + L \log |\Sigma|) 的,因为有一棵 trie 要用 map 存儿子。

首先观察到,每个 (s_1, s_2) 只有一次机会。因为 (t_1, t_2) 肯定是前边后边一样,中间一段不一样的。(s_1, s_2) 必须长得和这个 (t_1, t_2) 差不多。可以见下图:

所以,我们把 s, t 都拆成 LCP 和剩下那段,称为 x 和 y。然后,你要做的就是,对于每个询问,求出 x 和 y 都是 t 的 x 和 y 的前缀的 s 的数量。

当然,可能有 s 的 y 匹配 t 的 y 到一半,没把中间部分匹配完的情况,这个情况可以记录中间那段的长度解决。

考虑这件事情怎么做。前缀这回事可以用 trie。所以我们先把 y 塞到 trie 里。此时查询的形式是,对于 trie 上的到根链,求 x 被 t 的 x 当前缀一类。此时是最精彩的一部分,我们离线,把 t 的 x, y 预处理出来,y 也插到 trie 里面,对 trie 进行 dfs,第一次访问时插入 x 到另一棵 trie 里并处理询问,回溯时把 x 删掉。就可以避免重复查询,优化复杂度。

各种做法中这个代码应该算是比较好写的一种,2k 多一点的样子。

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector < int >;
using pii = pair < int, int >;
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(), (a).end()
#define qio() cin.tie(0) -> sync_with_stdio(0)
#define L(i, j, k) for (int i = j; i <= int(k); i++)
#define R(i, j, k) for (int i = j; i >= int(k); i--)

const int N = 2e5 + 5, L = 5e6 + 6, M = L << 1, S = 26;

struct node {
    string pre, sufs, suft;
    int len;
};
struct trie {
    int tot, ch[M][S], cnt[M];
    void modify(string s, int dt, int u = 0) {
        for (auto w : s) {
            int c = w - 'a';
            if (!ch[u][c]) ch[u][c] = ++tot;
            u = ch[u][c];
        }
        cnt[u] += dt;
    }
    int query(string s, int u = 0, int res = 0) {
        for (auto w : s) {
            int c = w - 'a';
            res += cnt[u];
            u = ch[u][c];
            if (!u) return res;
        }
        res += cnt[u];
        return res;
    }
} tr;

int get(char x, char y) {
    return (x - 'a') * 26 + y - 'a';
}

int n, q, tot, ans[N], rt[L];
map < int, int > ch[M];
vi Q[M];
string prep[N], preq[N];

node solve(string s, string t) {
    int P = 0, S = sz(s) - 1;
    string pre = "", sufs = "", suft = "";
    for (; s[P] == t[P]; pre += s[P++]);
    reverse(all(pre));
    for (; s[S] == t[S]; S--);
    L(i, P, sz(s) - 1) sufs += s[i], suft += t[i];
    return {pre, sufs, suft, S - P + 1};
}

inline void insert(int& u, int pos, string& s, string& t, int id) {
    if (!u) u = ++tot;
    if (pos == sz(s)) {
        Q[u].pb(id);
        return;
    }
    insert(ch[u][get(s[pos], t[pos])], pos + 1, s, t, id);
}
inline void dfs(int u) {
    if (!u) return;
    for (auto id : Q[u]) {
        if (id <= n) tr.modify(prep[id], 1);
        else ans[id - n] = tr.query(preq[id - n]);
    }
    for (auto [key, v] : ch[u]) dfs(v);
    for (auto id : Q[u]) {
        if (id <= n) tr.modify(prep[id], -1);
    }
}

int main() {
    qio(), cin >> n >> q;
    L(i, 1, n) {
        string s, t;
        cin >> s >> t;
        if (s == t) continue;
        auto [pre, sufs, suft, len] = solve(s, t);
        prep[i] = pre;
        insert(rt[len], 0, sufs, suft, i);
    }
    L(i, 1, q) {
        string s, t;
        cin >> s >> t;
        if (sz(s) != sz(t)) continue; // ... 
        auto [pre, sufs, suft, len] = solve(s, t);
        preq[i] = pre;
        insert(rt[len], 0, sufs, suft, i + n);
    }
    L(i, 1, L - 1) dfs(rt[i]);
    L(i, 1, q) cout << ans[i] << '\n';
    return 0;
}