题解:AT_arc081_c [ARC081E] Don't Be a Subsequence
首先确定答案长度是容易的。从前往后/从后往前扫一遍,记录一下当前哪些字符用过,如果所有字符都用过就把答案加一并清空所有字符的记录。
这个东西的实际意义就是你要让所有长度为
确定了长度
如果当前选出来的前缀不为 a。
否则我们找到最小的字符,使得加上这个字符后仍然有解。
记录
我们容易通过跳
时间复杂度
::::info[Code]
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,last[N][26],cnt[N];
string s;
bitset<26>use;
signed main(){
cin>>s;
n=s.size();
s=' '+s;
for(int i=n;i>=1;--i){
for(int j=0;j<26;++j)last[i][j]=last[i+1][j],cnt[i]=cnt[i+1];
last[i][(int)(s[i]-'a')]=i;
use[(int)(s[i]-'a')]=i;
if(use.count()==26){
use.reset();
++cnt[i];
}
}
int now=1;
for(int i=1;i<=cnt[1]+1;++i){
if(now>n){
cout<<"a";
continue;
}
for(int j=0;j<26;++j){
if(!last[now][j]){
cout<<((char)(j+'a'));
break;
}
if(i+cnt[last[now][j]+1]>=cnt[1]+1)continue;
cout<<((char)(j+'a'));
now=last[now][j]+1;
break;
}
}
cout<<"\n";
return 0;
}
::::