greedy strings *1100

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//Printing statement
#define el endl
#define printArray(v) for(auto k:v)cout<<k<< <<;
//STL operation
#define pb push_back
#define in insert
#define all(v) v.begin(),v.end()
#define asc(v) sort(all(v))
#define dsc(v) asc(v),reverse(all(v))
#define AvaDe ios_base::sync_with_stdio(false), cin.tie(NULL),cout.tie(NULL);
// BAKI SAB THEEK BAS CHAL RAHA HAI BRO...
bool comp(string a, string b)
{
 if (a + b < b + a)
 return true;
else
return false;
}
int prime(vector<int> v, int n)
{
int ct = 0;
vector<int> vv(n, true);
for (int i = 2; i <= n; i++)
{
if (vv[i] == true)
 ct++;
for (int j = 2 * i; j <= n; j = j + i)
{
vv[j] = false;
}
}
return ct;
// it returns only count.
}
bool comper(pair<int,int>&p1,pair<int,int>&p2)
{
//Return it in the same order in which it's required
 return p1.second<p2.second;
}
int findLastIndex(string& str, char x,int ind)
{
    int index = ind;
    for (int i = ind; i < str.length(); i++)
        if (str[i] == x)
            index = i;
            if(ind==str.size()-1){
                return -1;
            }
            else{
              return index;
            }
}
void solve(){
    string s;
    cin>>s;
    if(s.size()==1)
    {
        cout<<s;
        return;
    }
    set<char>s1;
    vector<char>v;
    for (size_t i = 0; i < s.size(); i++)
    {
       s1.insert(s[i]);
    }
    for(auto i:s1){
        v.pb(i);
    }
    reverse(v.begin(),v.end());
    int j=0;
    string ans="";
    int ind=0;
    set<int>vv;
    while(findLastIndex(s,v[j],ind)!=-1)
    {       
    int q=findLastIndex(s,v[j],ind);
    j++;
    ind=q;
    vv.insert(q); 
    }
    vector<int>v2;
    for(auto i:vv){
        v2.pb(i);
    }
    int ct=0;
    // for(auto i:v2){
    //     cout<<i<<" ";
    // 
     map<char,int>m;
      char ch='a';
            for(int j=0;j<=v2[0];j++)
        {
            m[s[j]]++;
            ch=max(ch,s[j]);
        }
        // cout<<ch;
        for(int k=0;k<m[ch];k++)
        {
            ans+=ch;
        }
        ct++;
        // cout<<ans<<el;
        // cout<<" hi";
    for(int i=0;i<v2.size()-1;i++)
    {
        map<char,int>mp;
        char ch='a';
        // else{
            for(int j=v2[i]+1;j<=v2[i+1];j++)
            {
                mp[s[j]]++;
            ch=max(ch,s[j]);
            }
            for(int k=0;k<mp[ch];k++)
        {
            ans+=ch;
        }
        // }
    //     for(auto i:mp)
    // cout<<i.first<<" "<<i.second<<el;
    }
    // for(auto i:vv)
    // cout<<i<< " "<<el;
    // cout<<s.size();
    cout<<ans;
}
int main()
{
AvaDe
int t=1;
// cin>>t;
while(t--){
solve();
}
}


Comments

Submit
0 Comments
More Questions

320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST