Lexicographically Maximum Subsequence

Lexicographically Maximum Subsequence

TimeLimit:2000MS  MemoryLimit:256MB

64-bit integer IO format:%I64d

Problem Description

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.

SampleInput 1

ababba

SampleOutput 1

bbba

SampleInput 2

abbcbccacbbcbaaba

SampleOutput 2

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

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
typedef long long ll;

const int N = 100005;
char a[N];
char b[N];
/*要是字典序最大,当然首字母要是整段字符串中最大的字符
那么我们找到这个最大的字符之后就要取出字符后面那串个字符串中最大的字符……
直到无法再取为止
所以一个字符要输出的充要条件是从这个字符开始的后缀最大字符就是本身
直接一个后缀和完事*/
//总结  学习到了b数组的后缀和  该递推关系可以用来预处理 从i开始后的最大值  可用来预处理
int main()
{
    scanf("%s", a);
    int len = strlen(a);
    int i = len-1;
    for (i = len - 1; i >= 0; i--)//预处理 b[i]表示 从i到len中的最大值
    {
        b[i] = max(a[i], b[i + 1]);
    }
    for (i = 0; i < len; i++)//输出最大值即可
    {
        if (a[i] == b[i])
        {
            printf("%c", a[i]);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值