题解:P5841 [CTSC2011] 字符串重排

· · 题解

我咋模拟赛场切了?

题目难度来自码量,比较有趣的一点是树高是根号的小观察。

给每个串的末尾增加一个特殊字符,每个串就都是一个叶子,P^* 等价于树的 DFS 序提取叶子。

把树上所有只有一个儿子的节点压缩没,可以证明树高是 O(\sqrt L) 的。

限制显然越靠后的价值越高,那思路显然就是:从后往前判断一个限制是否可以被满足,如果可以,就标记一下。

一个限制被满足意味着 DFS 的过程中需要干什么?设 lca 为 x 和 y 的最近公共祖先,xt 和 yt 是 x 和 y 不断往上跳到 lca 前的一个节点,因为 x\neq y 而且都是叶子,所以必定存在。

那么意思就是 DFS 到 tx 后需要最后 DFS 到 x,y 方向反之,lca 的儿子顺序中 tx 必须在 ty 的紧前面。

我们最后的 DFS 顺序可以通过每一个节点的儿子们的顺序唯一确定,儿子们的顺序在添加限制的过程中会加入形如“某一个在某一个紧前面”“某一个第一个访问”“某一个最后一个访问”这样的限制,考虑对于每一组兄弟维护一个链表森林。因为树高不大,可以暴力枚举全部可能添加有关儿子访问顺序的限制的节点,看看添加限制后是否合法。

最后统计答案的时候先确定每一个节点的儿子们的访问顺序,具体的,把限制必须放在开头的一段链表放在开头,限制必须放在结尾的一段链表放在结尾,其他链表放在中间,顺序任意。

模拟这个过程不精细实现应该是 O(q\sqrt L\Sigma) 的。(好像 \Sigma 在根号里面?尝试构造卡满的数据没有成功卡到那么满,但是字符集大小也不大,不在意。)

代码有点长。

:::success[代码]

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

int LCP(const string &a, string &b) {
    for (int i = 0; i < a.size() && i < b.size(); i++) {
        if (a[i] != b[i]) {
            return i;
        }
    }
    return min(a.size(), b.size());
}

#define MAXN 500005
#define int long long
int n, q, query_x[MAXN], query_y[MAXN];
string s[MAXN];
int cnt = 1, node_id[MAXN], trie_son[MAXN][27], string_id[MAXN];
vector<int> son[MAXN], son_left[MAXN], son_right[MAXN];
// son_left son_right : =-1 null ; >=0 node ; =-2 begin/end
int dep[MAXN], fa[MAXN], begin_son[MAXN], end_son[MAXN];
int son_id[MAXN]; // son[fa[x]][son_id[x]] == x
int ans;

int dfs_build(int u) { // return u's subtree in trie's root
    // cout << "dfs_build " << u << endl;
    for (int i = 0; i < 27; i++) {
        if (trie_son[u][i]) {
            // cout << u << " +" << i << " => " << trie_son[u][i] << endl;
            int s = dfs_build(trie_son[u][i]);
            son_id[s] = son[u].size();
            son[u].push_back(s);
        }
    }
    if (son[u].empty()) {
        return u;
    }
    if (son[u].size() == 1) {
        return son[u][0];
    }
    son_right[u] = vector<int>(son[u].size(), -1);
    son_left[u] = vector<int>(son[u].size(), -1);
    return u;
}
// son, son_id

void dfs_basic_data(int u) {
    for (int v : son[u]) {
        dep[v] = dep[u] + 1;
        fa[v] = u;
        dfs_basic_data(v);
    }
    /*
    cout << u << " =>";
    for (int v : son[u]) {
        cout << ' ' << v;
    }
    cout << endl;
    cout << "dep[" << u << "] = " << dep[u] << endl;
    cout << "fa[" << u << "] = " << fa[u] << endl;
    cout << "son_id[" << u << "] = " << son_id[u] << endl;
    */
}
// dep fa

bool check_point(int u) {
    vector<int> vis(son[u].size(), 0);
    int cnt_son = 0;
    if (begin_son[u] != -1) {
        int x = begin_son[u];
        while (x >= 0) {
            vis[x] = 1;
            cnt_son++;
            x = son_right[u][x];
        }
        if (x == -2) {
            goto CHECK_OVER;
        }
    }
    if (end_son[u] != -1) {
        int x = end_son[u];
        while (x >= 0) {
            vis[x] = 1;
            cnt_son++;
            x = son_left[u][x];
        }
    }
    for (int i = 0; i < son[u].size(); i++) {
        if (!vis[i] && son_left[u][i] == -1) {
            int x = i;
            while (x >= 0) {
                vis[x] = 1;
                cnt_son++;
                x = son_right[u][x];
            }
        }
    }
    CHECK_OVER:;
    return cnt_son == son[u].size();
}

bool check(int x, int y) {
    int lca = 0, xtop = 0, ytop = 0;
    {
        int xx = x, yy = y;
        if (dep[xx] < dep[yy]) {
            swap(xx, yy);
        }
        // cout << __LINE__ << endl;
        while (dep[xx] > dep[yy]) {
            xx = fa[xx];
        }
        // cout << __LINE__ << ' ' << x << ' ' << y << endl;
        while (xx != yy) {
            xtop = xx;
            ytop = yy;
            xx = fa[xx];
            yy = fa[yy];
            // cout << xx << ' ' << yy << endl;
        }
        // cout << __LINE__ << endl;
        if (dep[x] < dep[y]) {
            swap(xtop, ytop);
        }
        // cout << __LINE__ << endl;
        lca = xx;
    }
    // cout << "check " << x << ' ' << y << "    " << lca << ' ' << xtop << ' ' << ytop << endl;
    {
        if (son_right[lca][son_id[xtop]] != -1 && son_right[lca][son_id[xtop]] != son_id[ytop]) return 0;
        if (son_left[lca][son_id[ytop]] != -1 && son_left[lca][son_id[ytop]] != son_id[xtop]) return 0;
        int o1 = son_right[lca][son_id[xtop]];
        son_right[lca][son_id[xtop]] = son_id[ytop];
        int o2 = son_left[lca][son_id[ytop]];
        son_left[lca][son_id[ytop]] = son_id[xtop];
        bool OK = check_point(lca);
        son_right[lca][son_id[xtop]] = o1;
        son_left[lca][son_id[ytop]] = o2;
        if (!OK) return 0;
    }
    // cout << "check LCA OK" << endl;
    {
        // x->xtop-1 need to be the end
        while (x != xtop) {
            if (son_right[fa[x]][son_id[x]] != -1 && son_right[fa[x]][son_id[x]] != -2) return 0;
            if (end_son[fa[x]] != -1 && end_son[fa[x]] != son_id[x]) return 0;
            int o1 = son_right[fa[x]][son_id[x]];
            son_right[fa[x]][son_id[x]] = -2;
            int o2 = end_son[fa[x]];
            end_son[fa[x]] = son_id[x];
            bool OK = check_point(fa[x]);
            son_right[fa[x]][son_id[x]] = o1;
            end_son[fa[x]] = o2;
            if (!OK) return 0;
            x = fa[x];
        }
    }
    // cout << "check x OK" << endl;
    {
        // y->ytop-1 need to be the begin
        while (y != ytop) {
            if (son_left[fa[y]][son_id[y]] != -1 && son_left[fa[y]][son_id[y]] != -2) return 0;
            if (begin_son[fa[y]] != -1 && begin_son[fa[y]] != son_id[y]) return 0;
            int o1 = son_left[fa[y]][son_id[y]];
            son_left[fa[y]][son_id[y]] = -2;
            int o2 = begin_son[fa[y]];
            begin_son[fa[y]] = son_id[y];
            bool OK = check_point(fa[y]);
            son_left[fa[y]][son_id[y]] = o1;
            begin_son[fa[y]] = o2;
            if (!OK) return 0;
            y = fa[y];
        }
    }
    // cout << "check y OK" << endl;
    return 1;
}

void mark(int x, int y) {
    int lca = 0, xtop = 0, ytop = 0;
    {
        int xx = x, yy = y;
        if (dep[xx] < dep[yy]) {
            swap(xx, yy);
        }
        while (dep[xx] > dep[yy]) {
            xx = fa[xx];
        }
        while (xx != yy) {
            xtop = xx;
            ytop = yy;
            xx = fa[xx];
            yy = fa[yy];
        }
        if (dep[x] < dep[y]) {
            swap(xtop, ytop);
        }
        lca = xx;
    }
    {
        son_right[lca][son_id[xtop]] = son_id[ytop];
        son_left[lca][son_id[ytop]] = son_id[xtop];
    }
    {
        while (x != xtop) {
            son_right[fa[x]][son_id[x]] = -2;
            end_son[fa[x]] = son_id[x];
            x = fa[x];
        }
    }
    {
        while (y != ytop) {
            son_left[fa[y]][son_id[y]] = -2;
            begin_son[fa[y]] = son_id[y];
            y = fa[y];
        }
    }
}
// son_left, son_right, begin_son, end_son

vector<int> P;
void dfs_ans(int u) {
    // cout << "dfs_ans " << u << endl;
    if (string_id[u]) {
        P.push_back(string_id[u]);
        return;
    }
    vector<int> vis(son[u].size(), 0), order, end_order;
    if (begin_son[u] != -1) {
        int x = begin_son[u];
        while (x >= 0) {
            order.push_back(x);
            vis[x] = 1;
            x = son_right[u][x];
        }
        if (x == -2) {
            goto ORDER_OVER;
        }
    }
    if (end_son[u] != -1) {
        int x = end_son[u];
        while (x >= 0) {
            end_order.push_back(x);
            vis[x] = 1;
            x = son_left[u][x];
        }
    }
    for (int i = 0; i < son[u].size(); i++) {
        if (!vis[i] && son_left[u][i] == -1) {
            int x = i;
            while (x >= 0) {
                order.push_back(x);
                vis[x] = 1;
                x = son_right[u][x];
            }
        }
    }
    while (!end_order.empty()) {
        order.push_back(end_order.back());
        end_order.pop_back();
    }
    ORDER_OVER:;
    /*
    cout << "order =";
    for (int v : order) {
        cout << ' ' << v;
    }
    cout << endl;
    */
    for (int v : order) {
        dfs_ans(son[u][v]);
    }
}

signed main() {
    memset(begin_son, -1, sizeof(begin_son));
    memset(end_son, -1, sizeof(end_son));
    // freopen("reorder.in", "r", stdin);
    // freopen("reorder.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i].push_back('`');
        int &x = node_id[i] = 1;
        for (char j : s[i]) {
            if (!trie_son[x][j - '`']) {
                trie_son[x][j - '`'] = ++cnt;
            }
            x = trie_son[x][j - '`'];
        }
        string_id[x] = i;
        // cout << i << ' ' << x << endl;
    }
    for (int i = 1; i <= q; i++) {
        cin >> query_x[i] >> query_y[i];
    }
    int root = dfs_build(1);
    dep[root] = 1;
    dfs_basic_data(root);
    // cout << "\n\n\n" << endl;
    vector<int> OK_query;
    for (int i = q; i >= 1; i--) {
        // query_x[i]++;
        // query_y[i]++;
        // cout << i << ' ' << query_x[i] << ' ' << query_y[i] << endl;
        if (check(node_id[query_x[i]], node_id[query_y[i]])) {
            // cout << "\t\t\tOK " << i << endl;
            OK_query.push_back(i);
            mark(node_id[query_x[i]], node_id[query_y[i]]);
        }
    }
    // return 0;
    // return 0;
    dfs_ans(root);
    for (int i = 1; i < n; i++) {
        ans += LCP(s[P[i]], s[P[i - 1]]) * LCP(s[P[i]], s[P[i - 1]]);
        // cout << LCP(s[P[i]], s[P[i - 1]]) << endl;
    }
    cout << ans << endl;
    reverse(OK_query.begin(), OK_query.end());
    cout << OK_query.size();
    for (int i : OK_query) {
        cout << ' ' << i;
    }
    cout << endl;
    for (int i : P) {
        cout << i << ' ';
    }
    cout << endl;
    /*
    for (int i = 32; i <= 127; i++) {
        cout << i << " [" << char(i) << "]\n";
    }
    */
    cout.flush();
    return 0;
}

:::