197C Codeforces - Lexicographically Maximum Subsequence


197C codeforces - Lexicographically Maximum Subsequence






Solution in C++


#include <bits/stdc++.h>

using namespace std;
#define pii pair<int,int>
typedef long long int lli;

string inp,sol;
map<char,int> lastof;
int sz;
char mx;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>inp;
    sz=inp.size();
    for(int i=0;i<sz;i++)
        mx=max(mx,inp[i]),
        lastof[inp[i]]=i;

    for(int i=0;i<sz;i++)
    {
        while(i>lastof[mx])mx--;
        if(i<=lastof[mx] && inp[i]==mx)sol+=mx;
    }
    cout<<sol;
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق