999C - Alphabetic Removals - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

n,k = map(int,input().strip().split())
s = input()
l = n
i=97
while k!=0 and i<123:
	c = chr(i)
	s = s.replace(c,'',k)
	nl = len(s)
	k -= (l-nl)
	l = nl
	i+=1
print(s)

C++ Code:

//This is coded by NISHANTH VM
//#include <bits/stdc++.h>
#include <iostream> 
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int,int> mii;
typedef map<int,v32> miv;
ll MOD = 998244353;
double eps = 1e-12;
#define rep(i,e) for(ll i = 0; i < e; i++)
#define repFR(i,s,e) for(ll i = s; i < e; i++)
#define repR(i,s) for(ll i = s; i >= 0; i--)
#define repRR(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
 

void solve(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n,k;
cin>>n>>k;
string s;
cin>>s;
map<char,int> m;
for(char a:s){
    m[a]++;
}
for(auto i:m){
    if(k>0){
        if(i.second <= k ){
        k = k - i.second;
        for(int j=0;j<n;j++){
            if(s[j]==i.first) s[j] = 'A';
        }
        }
        else{
            int ref1 = k ;
            k = k - i.second;
            for(int j=0;j<n;j++){
                if(s[j]==i.first && ref1!=0){
                    s[j]='A';
                    ref1--;
                }
                if(ref1==0){
                    break;
                }              
            } 
        }
    }
    else{
        break;
    }
}
    for(char a:s){
        if(a!='A') cout<<a;
    }
    cout<<"\n";
}
int main()
{
fast_cin();
ll t;
// cin >> t;
for(int it=1;it<=1;it++) {
 //cout << "Case #" << it+1 << ": ";
 solve();
 }
 return 0;
}


Comments

Submit
0 Comments
More Questions

1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know