[CodeForces - 197C] C - Lexicographically Maximum Subsequence

C - Lexicographically Maximum Subsequence

You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.

We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|.

String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes.

Input

The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.

Output

Print the lexicographically maximum subsequence of string s.

Example

Input
ababba
Output
bbba
Input
abbcbccacbbcbaaba
Output
cccccbba

Note

Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).

The first sample: aBaBBA

The second sample: abbCbCCaCbbCBaaBA

 

这个题目就是要求出子串中字典序最大的.

由于本题有许多方法,那我就讲一讲我自己的好了.

由于我们要尽量挑字典序最大的字符作为开头(首关键字并不是长度!),所以,我们要记录当前字符串的最大字符,然后,如果剩下的字符里面,已经不存在这个最大的字符,那么这个字符就下调,知道存在.

这样一个纯模拟的过程就好了,不过不知道为什么我在比赛中时间用的稍微有点多......

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int len,las[105],nxt[100005],R[105];
 6 char a[100005],c;
 7 int read(){
 8     int x=0,f=1; char ch=getchar();
 9     while (ch<'0'||ch>'9'){if (ch=='-') f=-f; ch=getchar();}
10     while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
11     return x*f;
12 }
13 int main(){
14     scanf("%s",a),len=strlen(a),c='a';
15     memset(las,255,sizeof las);
16     memset(nxt,0,sizeof nxt);
17     for (int i=len-1; i>=0; i--) if (las[a[i]-'a']==-1) las[a[i]-'a']=i,nxt[i]=i,R[a[i]-'a']=i;
18     else nxt[i]=las[a[i]-'a'],las[a[i]-'a']=i;
19     for (int i=0; i<len; i++) if (c<a[i]) c=a[i];
20     for (int i=0; i<len; i++) if (a[i]==c){
21         putchar(c); if (nxt[i]!=i) continue;
22         while (c>'a'&&R[c-'a']<=i) c=(char)((int)c-1);
23     }
24     return 0;
25 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7214382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值