lexicographically largest subsequence

Time limit: 1 second for C/C++, 2 seconds for other languages
Space limit: C/C++ 32768K, other languages ​​65536K
64bit IO Format: %lld

Topic description 

Given a string s containing only lowercase letters, request the lexicographically largest subsequence.
Subsequence: https://en.wikipedia.org/wiki/Subsequence Lexicographical
order: https://en.wikipedia.org/wiki/Lexicographical_order

Enter description:

One string per line (1 <= |s| <= 100,000).

Output description:

The lexicographically largest subsequence.
Example 1

enter

ababba

output

bbba
Example 2

enter

abbcbccacbbcbaaba

output

cccccbba


#include<bits/stdc++.h>
using namespace std;
intmain()
{
    string s,ans;
    int index=0;
    cin>>s;
        for(int j='z'; j>='a'; j--)
            for(int i=0; i<s.size(); i++)
                if(s[i]==j&&i>=index)
                    ans+=j,index=i;
        cout<<ans<<endl;
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685261&siteId=291194637