code forces 999C Alphabetic Removals

C. Alphabetic Removals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (knk≤n) from the string ss. Polycarp uses the following algorithm kk times:c++

  • if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
  • if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
  • ...
  • remove the leftmost occurrence of the letter 'z' and stop the algorithm.

This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly kk times, thus removing exactly kk characters.數組

Help Polycarp find the resulting string.ide

Input

The first line of input contains two integers nn and kk (1kn41051≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.this

The second line contains the string ss consisting of nn lowercase Latin letters.spa

Output

Print the string that will be obtained from ss after Polycarp removes exactly kk letters using the above algorithm kk times.code

If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).orm

Examples
input
Copy
15 3
cccaabababaccbc
output
Copy
cccbbabaccbc
input
Copy
15 9
cccaabababaccbc
output
 
cccccc
input
 
1 1
u
output
u
 
題意:給你一個字符串,你能夠對這個字符串作k次操做,每次操做遵循這個規則:從a開始刪除、刪完a後刪b...一直刪到z
題解:用一個數組來存字符串中各個字母的個數,而後從a開始刪,記錄到不能刪除的那個位置而後用另外一個字符串來保存輸出
代碼以下
#include<bits/stdc++.h>
using namespace std; int n; string str; int k; vector<int> cnt(26); int main() { cin>>n>>k; cin>>str; for(int i=0; i<n; i++) { cnt[str[i]-'a']++; } //記錄下每一個字母的數量 //從a開始減去存在的字母 //若是沒有了 就記錄下還能夠輸出的字母
    int pos=26; for(int i=0; i<26; i++) { if(k>=cnt[i]) { k-=cnt[i]; } else { pos=i; break; } } string ans; int rem=k; for(int i=0; i<n; i++) { int cur=str[i]-'a'; //在pos後面的字母能夠用 //用完了k的字母能夠用
        
        if(cur>pos||(cur==pos&&rem==0)) { ans+=str[i]; } else if(cur==pos) { rem--; } } cout<<ans<<endl; return 0; }
View Code
相關文章
相關標籤/搜索