考虑 LCS 的 dp 是, 序列匹配到第 个数, 序列匹配到第 个数,LCS 的长度是多少。
那么自然的。加上一个状态,表示 内的位是否翻转。这样是 。
你注意到 肯定不超过 与答案的差,而考虑如下匹配规则,能让答案至少为 :
- 对于 的位置 ,令 为 当且仅当 。
这样是 ,还是爆了,进一步的,我们分段,对 但 的位置,我们在 后划一条分割线,这样我们把序列分成若干长度为大于等于 的段(除了开头)。
我们只需重新划分使得:除了开头的段,每段的长度都大于 ,且只有一个没被匹配上,就能证明答案大于等于 。
调整相对容易,对于两个相邻的长度为 的段,本质不同的情况只有这两种,考虑这样匹配:
这样,我们不断消去结尾的能被消除的长度为 的段,直到每个长度为 的段都接在一个未被调整的段后面。
大概是这样:
那就可以这样匹配:
此时长度为 的段(除了开头)就一定会被消去。
我们就证明了答案大于等于 ,就做到 。
上述结论显然可以推广到:对前面只有一个 的位置的段进行收缩。如果我们预先把段缩起来,根据得到的答案下界设定 的最大值,采用记忆化搜索的形式,只记录 是否翻转,应该还能分析到更优。
代码
// 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();
}