跳到正文

题解

题解:AT_arc157_f [ARC157F] XY Ladder LCS

_Z_F_R_约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年6月24日
收录
2026年6月25日
更新
2026年11月10日
篇幅
约 9 分钟

版本

  1. v1当前

考虑 LCS 的 dp 是,xx 序列匹配到第 ii 个数,yy 序列匹配到第 jj 个数,LCS 的长度是多少。

那么自然的。加上一个状态,表示 [min⁡(i,j),max⁡(i,j)][\min(i, j), \max(i, j)] 内的位是否翻转。这样是 O(n2n)O(n2^n)。

你注意到 ∣i−j∣|i - j| 肯定不超过 nn 与答案的差,而考虑如下匹配规则,能让答案至少为 ⌊n2⌋\lfloor \frac{n}{2} \rfloor:

  • 对于 Si≠TiS_i \not = T_i 的位置 ii,令 SiS_i 为 X\texttt{X} 当且仅当 i mod 2=0i \bmod 2 = 0。

这样是 O(n2n2)O(n2^{\frac{n}{2}}),还是爆了,进一步的,我们分段,对 Si=TiS_i = T_i 但 Si−1≠Ti−1S_{i - 1} \not = T_{i - 1} 的位置,我们在 i−1i - 1 后划一条分割线,这样我们把序列分成若干长度为大于等于 22 的段(除了开头)。

我们只需重新划分使得:除了开头的段,每段的长度都大于 22,且只有一个没被匹配上,就能证明答案大于等于 n−⌈n3⌉n - \lceil \frac{n}{3} \rceil。

调整相对容易,对于两个相邻的长度为 22 的段,本质不同的情况只有这两种,考虑这样匹配:

XXXXXYXY\texttt{\color{red}XXX\color{blue}X} \\ \texttt{\color{blue}X\color{black}Y\color{blue}X\color{black}Y} XXYXXYYY\texttt{\color{black}X\color{red}XY\color{black}X} \\ \texttt{\color{blue}XYY\color{red}Y}

这样,我们不断消去结尾的能被消除的长度为 22 的段,直到每个长度为 22 的段都接在一个未被调整的段后面。

大概是这样:

XXXXXXYYYYXY\texttt{XXXXXX} \\ \texttt{YYYYXY}

那就可以这样匹配:

XXXXXXYYYYXY\texttt{\color{black}X\color{blue}{X}\color{red}X\color{blue}{X}\color{red}XX} \\ \texttt{\color{blue}Y\color{red}Y\color{blue}{Y}\color{red}Y\color{blue}X\color{black}Y}

此时长度为 22 的段(除了开头)就一定会被消去。

我们就证明了答案大于等于 n−⌈n3⌉n - \lceil\frac{n}{3}\rceil,就做到 O(n2n3)O(n2^{\frac{n}{3}})。

上述结论显然可以推广到:对前面只有一个 Si=TiS_i = T_i 的位置的段进行收缩。如果我们预先把段缩起来,根据得到的答案下界设定 ∣i−j∣|i - j| 的最大值,采用记忆化搜索的形式,只记录 Si≠TiS_i \not = T_i 是否翻转,应该还能分析到更优。

代码
C++
// Think TWICE, code ONCE
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll Read() {
    int sig = 1; ll num = 0; char c = getchar();
    while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
    while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
    return num * sig;
}
void Write(ll x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) Write(x / 10);
    putchar((x % 10) ^ 48);
}
const int N = 55;
int n, lim, s[N], t[N];
struct tNode {
    int len; ll val;
    friend bool operator <(tNode x, tNode y) { return x.len == y.len ? x.val > y.val : x.len < y.len; }
    void Output() { for(int i = len - 1; i >= 0; i--) putchar('X' + ((val >> i) & 1)); }
    void OutputE() { for(int i = len - 1; i >= 0; i--) cerr << char('X' + ((val >> i) & 1)); }
};
tNode Append(int c, tNode x) { return {x.len + 1, ((ll)c << x.len) + x.val}; }
map<ll, tNode> mp;
tNode DP(int x, int y, ll S) {
    if(abs(x - y) > lim) return {0, 0};
    ll val = (S >> min(x, y)) * N * N + x * N + y;
    if(mp.count(val)) return mp[val];
    int o; tNode res = {0, 0};
    if(x < n) {
        if(x >= y) {
            res = max(res, DP(x + 1, y, S));
            if(s[x + 1] != t[x + 1]) res = max(res, DP(x + 1, y, S | (1ll << (x + 1))));
        }
        else res = max(res, DP(x + 1, y, S & (~(1ll << (x + 1)))));
    }
    if(y < n) {
        if(x <= y) {
            res = max(res, DP(x, y + 1, S));
            if(s[y + 1] != t[y + 1]) res = max(res, DP(x, y + 1, S | (1ll << (y + 1))));
        }
        else res = max(res, DP(x, y + 1, S & (~(1ll << (y + 1)))));
    }
    if(x < n && y < n) {
        if(x == y) { if(s[x + 1] == t[x + 1]) res = max(res, Append(s[x + 1], DP(x + 1, y + 1, S))); }
        else if(x < y) {
            for(o = 0; o <= (s[y + 1] != t[y + 1]); o++) {
                int cx = ((S >> (x + 1)) & 1) ? t[x + 1] : s[x + 1];
                int cy = o ? s[y + 1] : t[y + 1];
                if(cx == cy) res = max(res, Append(cx, DP(x + 1, y + 1, (S & (~(1ll << (x + 1)))) | ((ll)o << (y + 1)))));
            }
        }
        else {
            for(o = 0; o <= (s[x + 1] != t[x + 1]); o++) {
                int cy = ((S >> (y + 1)) & 1) ? s[y + 1] : t[y + 1];
                int cx = o ? t[x + 1] : s[x + 1];
                if(cx == cy) res = max(res, Append(cy, DP(x + 1, y + 1, (S & (~(1ll << (y + 1)))) | ((ll)o << (x + 1)))));
            }
        }
    }
    return mp[val] = res;
}
int main() {
    int i, lst = 1; bool o = true; lim = n = Read(), lim--;
    char c = getchar();
    while(c != 'X' && c != 'Y') c = getchar();
    for(i = 1; i <= n; i++) s[i] = c == 'Y', c = getchar();
    while(c != 'X' && c != 'Y') c = getchar();
    for(i = 1; i <= n; i++) t[i] = c == 'Y', c = getchar();
    for(i = 2; i <= n + 1; i++) if(i == n + 1 || (s[i] == t[i] && s[i - 1] != t[i - 1])) {
        lim--;
        if(o && (lst < n && s[lst] == t[lst] && s[lst + 1] != t[lst + 1])) lim++, o = false;
        else o = true;
        lst = i;
    }
    lim = max(0, n - lim), DP(0, 0, 0).Output();
}
原文