AT2698 [ARC081C] Don‘t Be a Subsequence
https://www.luogu.com.cn/problem/AT2698
好久之前写的,一下子看题发现不太会做
看了之前的代码,然后就懂了
题解区写的是什么勾8,这么长
考虑倒过来做
设
f
[
i
]
f[i]
f[i]表示
i
i
i开始,往后的没有出现过的最短串长度
然后记个 n x t [ c ] nxt[c] nxt[c]表示下一个出现的 c c c在什么位置
枚举字母转移即可
code:
#include<bits/stdc++.h>
using namespace std;
char st[1000005];
int f[1000005], nxt[1000005], haa[1000005], ha[1000005];
int main() {
scanf("%s", st + 1);
int n = strlen(st + 1);
for(int i = 0; i < 26; i ++) nxt[i] = 1000000;
f[n + 1] = 1;
for(int i = n; i >= 1; i --) {
nxt[st[i] - 'a'] = i;
for(int j = 0; j < 26; j ++) {
if(f[i] == 0 || f[i] > f[nxt[j] + 1] + 1) f[i] = f[nxt[j] + 1] + 1, ha[i] = j, haa[i] = nxt[j] + 1;
}
}
int now = 1;
while(now <= n) {
printf("%c", char(ha[now] + 'a'));
now = haa[now];
}
return 0;
}