CF999C Alphabetic Removals 思维 第六道 水题

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:

  • 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 kkcharacters.

Help Polycarp find the resulting string.

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.

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

Output

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

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

Examples
input
Copy
15 3
cccaabababaccbc
output
Copy
cccbbabaccbc
input
Copy
15 9
cccaabababaccbc
output
Copy
cccccc
input
Copy
1 1
u
output
Copy

 

这三天写CF终于一遍过了一道题

题意: 给你n个字符,让你删除k个字符,从a开始删,从左到右,删完k个为止,全部删完输出“nothing”或者不输出

vis数组记录每个字符的个数,然后计算下删k个字符删到那个字符为止,记录下剩余这个字符的数量,将这个字符前面的字符的记录值归0

然后从后往前遍历,遇到vis数组还有值得就加入保存结果得字符串t

最后反着输出字符串t(因为你开始时从后往前遍历的,得到的是倒的字符串)

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
int main(){
    std::ios::sync_with_stdio(false);
    ll n, k;
    while( cin >> n >> k ) {
        string s;
        cin >> s;
        ll vis[50] = {0}, num = 0;
        for( ll i = 0; i < s.length(); i ++ ) {
            vis[s[i]-'a'] ++;
        }
        for( ll i = 0; i <= 25; i ++ ) {
            num += vis[i];
            if( num > k ) {
                vis[i] = vis[i] - ( k - ( num - vis[i] ) );
                break;
            } else {
                vis[i] = 0;
            }
        }
        string t = "";
        for( ll i = s.length()-1; i >= 0; i -- ) {
            if( vis[s[i]-'a'] ) {
                t += s[i];
                vis[s[i]-'a'] --;
            }
        }
        if( t.length() == 0 ) {
            cout << endl;
            continue;
        }
        for( ll i = t.length()-1; i >= 0; i -- ) {
            cout << t[i];
        }
        cout << endl;
    }
    return 0;
}

 

posted on 2018-06-22 19:09  九月旧约  阅读(183)  评论(0编辑  收藏  举报

导航