Codeforces 196D Lexicographically Maximum Subsequence(模拟+哈希)

97 篇文章 0 订阅
3 篇文章 0 订阅

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



题解:

很简单的一题,直接从最大的那个字符开始输出,用哈希存一下这之后所有字符的情况,每次直到最大的才输出就好了

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int a[30];
int b[30];
int main()
{
    char s[100005];
    int i,j,maxx=0,d=0,len;
    scanf("%s",s);
    memset(a,0,sizeof(a));
    len=strlen(s);
    for(i=0;s[i];i++)
    {
        if(maxx<s[i]-'a')
        {
            d=i;
            maxx=s[i]-'a';
            memset(a,0,sizeof(a));
        }
        a[s[i]-'a']++;
    }
    for(i=d;i<len;i++)
    {
        if(maxx==(s[i]-'a'))
            printf("%c",s[i]);
        a[s[i]-'a']--;
        if(a[maxx]==0)
        {
            while(maxx>0)
            {
                maxx--;
                if(a[maxx])
                {
                    break;
                }
            }
        }
    }
    printf("\n");
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值