6597: Don't Be a Subsequence
时间限制: 1 Sec 内存限制: 128 MB
提交: 227 解决: 40
[提交] [状态] [讨论版] [命题人:admin]
题目描述
A subsequence of a string S is a string that can be obtained by deleting zero or more characters from S without changing the order of the remaining characters. For example, arc, artistic and (an empty string) are all subsequences of artistic; abc and ci are not.
You are given a string A consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of A. If there are more than one such string, find the lexicographically smallest one among them.
Constraints
1≤|A|≤2×105
A consists of lowercase English letters.
输入
Input is given from Standard Input in the following format:
A
输出
Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of A.
样例输入
atcoderregularcontest
样例输出
b
提示
The string atcoderregularcontest contains a as a subsequence, but not b.
来源/分类
题意:给一个字串s,找出字典序最小并且不是s子序列的串。
分析:将从后向前将s划分为区间,每个区间包含一套26字母a~z。考虑如果划分为n个区间,那么答案串的长度为n+1。再在各个区间内讨论。
如果答案串的第一个字母出现在了(1 , l[1])区间内,那么与它组合的n+1个字母一定是s的子序列。因此第一个字母一定是(1 , l[1])中没出现的。
若第一个字母在s中x位置出现了,那么如果答案串的第二个字母在(x+1 , r[1])出现过,那么第一个和第二个字母组成的长度为n+1子串一定是s子序列,所以第二个字母一定为(x+1 , r[1])中没出现过的。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int l[200005],r[200005];
char s[200005],vis[30];
int cut=1,num=0;
int main()
{
scanf("%s",s+1);
int len=strlen(s+1);
r[1]=len;
for(int i=len;i>=1;i--)
{
if(!vis[s[i]-'a'])
{
num++;
vis[s[i]-'a']=1;
if(num==26)
{
l[cut]=i;
cut++;
r[cut]=i-1;
memset(vis,0,sizeof(vis));
num=0;
}
}
}
l[cut]=1;
int pos=1;
char p,x;
for(int i=1;i>=1;i--)
{
memset(vis,0,sizeof(vis));
for(int j=pos;j<=r[i];j++)
vis[s[j]-'a']=1;
for(int j=0;j<26;j++)
{
if(!vis[j])
{
printf("%c",j+'a');
p=j+'a';
break;
}
}
for(int j=pos;j<=len;j++)
{
if(s[j]==p)
{
pos=j+1;
break;
}
}
}
printf("\n");
return 0;
}