题解:P12773 [POI 2018/2019 R1] 马虎 Nonchalance
lihaochen123 · · 题解
:::info[闲话]
@lihaochen123 : 这,就是,预言,吗。
@lihaochen123 : 题目名:马虎。
@lihaochen123 : 我要申诉啊.
@lihaochen123 : 我怀疑我赛时测 nie-s 的时候运行的一直是 nie。 || @lihaochen123 : 我也不知道为啥赛时测出来 nie-s 没过大样例……我要【】搬题人。 || @lihaochen123 : 这期拉了,我把写了一半的正解保存为 nie.cpp,写完的正解保存为 nie-s.cpp,赛时以为 nie-s 过不了,赛后发现 nie-s.cpp 过了
唯一 rk1 的一次,还因为这种问题
未曾设想的挂分方式。
$ i=1
$ cp ../down/nie/large_samples/large.0$i.in ./nie.in; ./nie-s; ./nie-checker ./nie.in ./nie.out ./nie.out
ok The sequence is a valid solution
$ i=2
$ cp ../down/nie/large_samples/large.0$i.in ./nie.in; ./nie-s; ./nie-checker ./nie.in ./nie.out ./nie.out
ok The sequence is a valid solution
$ i=3
$ cp ../down/nie/large_samples/large.0$i.in ./nie.in; ./nie-s; ./nie-checker ./nie.in ./nie.out ./nie.out
ok The sequence is a valid solution
$ i=4
$ cp ../down/nie/large_samples/large.0$i.in ./nie.in; ./nie-s; ./nie-checker ./nie.in ./nie.out ./nie.out
ok The sequence is a valid solution
$ i=5
$ cp ../down/nie/large_samples/large.0$i.in ./nie.in; ./nie-s; ./nie-checker ./nie.in ./nie.out ./nie.out
ok The sequence is a valid solution
:::
要求两个字符串的一个极长公共子序列,即不能在某个位置插入一个字符使得还是两个字符串的公共子序列。
考虑判定。
对于
若存在
考虑先抽出所有的
则考虑首先
对于保证
求出
这样以此类推,我们就得到了一个正确但是
真的是
递归的时候传下标范围。设某一次求解时
:::info[rec&code]
rec。
有些许细节。
// XL-X
// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
int a[1000005], b[1000005];
vector<int> ap[4], bp[4];
int main()
{
string s, t;
cin >> s >> t;
int n = s.size(), m = t.size();
for(int i=1;i<=n;i++)
{
a[i] = (s[i-1] == 'A' ? 0 : (s[i-1] == 'C' ? 1 : (s[i-1] == 'G' ? 2 : 3)));
ap[a[i]].push_back(i);
}
for(int i=1;i<=m;i++)
{
b[i] = (t[i-1] == 'A' ? 0 : (t[i-1] == 'C' ? 1 : (t[i-1] == 'G' ? 2 : 3)));
bp[b[i]].push_back(i);
}
// 我是要写 pure 还是 maybe corrupeted.
// pure 吧。
// ……感觉差不多难写。
string ans;
U([&](auto &&self, int lvl, int sl, int sr, int tl, int tr) -> pair<int, int>
{
// Lorem ipsum dolor sit amet
if(sl > sr || tl > tr) return {sl, tl};
if(lvl == 4) return {sl, tl};
int c1r = upper_bound(ap[lvl].begin(), ap[lvl].end(), sr) - ap[lvl].begin(), c1l = lower_bound(ap[lvl].begin(), ap[lvl].end(), sl) - ap[lvl].begin(),
c2r = upper_bound(bp[lvl].begin(), bp[lvl].end(), tr) - bp[lvl].begin(), c2l = lower_bound(bp[lvl].begin(), bp[lvl].end(), tl) - bp[lvl].begin();
if(c1r - c1l <= c2r - c2l)
{
if(c1l == c1r) return self(self, lvl + 1, sl, sr, tl, tr);
int rs = sl - 1, rt = tl - 1;
for(int i=c1l-1;i<c1r;i++)
{
int xl = (i == c1l - 1 ? sl - 1 : ap[lvl][i]), xr = (i + 1 == c1r ? sr + 1 : ap[lvl][i + 1]);
int yl = rt, yr = (i + 1 == c1r ? tr + 1 : bp[lvl][c2r - (c1r - (i + 1))]);
if(i != c1l - 1) ans += s[ap[lvl][i] - 1];
auto [ps, pt] = self(self, lvl + 1, xl + 1, xr - 1, yl + 1, yr - 1); // consectetur adipisicing elit
rs = max({rs, ps, xl});
rt = max({rt, i + 1 == c1r ? pt : *lower_bound(bp[lvl].begin(), bp[lvl].end(), pt)});
}
// if(!(rs <= sr && rt <= tr)) __debugbreak();
return {min(sr, rs), min(tr, rt)};
}
else
{
if(c2l == c2r) return self(self, lvl + 1, sl, sr, tl, tr);
int rs = sl - 1, rt = tl - 1;
for(int i=c2l-1;i<c2r;i++)
{
int xl = (i == c2l - 1 ? tl - 1 : bp[lvl][i]), xr = (i + 1 == c2r ? tr + 1 : bp[lvl][i + 1]);
int yl = rs, yr = (i + 1 == c2r ? sr + 1 : ap[lvl][c1r - (c2r - (i + 1))]);
if(i != c2l - 1) ans += t[bp[lvl][i] - 1];
auto [ps, pt] = self(self, lvl + 1, yl + 1, yr - 1, xl + 1, xr - 1);
rs = max({rs, i + 1 == c2r ? ps : *lower_bound(ap[lvl].begin(), ap[lvl].end(), ps)});
rt = max({rt, pt, xl});
}
// if(!(rs <= sr && rt <= tr)) __debugbreak();
return {min(sr, rs), min(tr, rt)};
}
})(0, 1, n, 1, m);
printf("%s\n", ans.c_str());
return 0;
}
/*
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
*/
:::