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 (k≤nk≤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 kk characters.

Help Polycarp find the resulting string.

Input

The first line of input contains two integers nn and kk (1≤k≤n≤4?1051≤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 3cccaabababaccbc

output

Copy

cccbbabaccbc

input

Copy

15 9cccaabababaccbc

output

cccccc

input

1 1u

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;
}



原文地址:https://www.cnblogs.com/buerdepepeqi/p/9216842.html

时间: 2024-10-16 06:45:44

code forces 999C Alphabetic Removals的相关文章

贪心-Code forces -387B -George and Round

Code forces -387B -George and Round description George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer 

Code Forces Gym 100971D Laying Cables(单调栈)

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

Code Forces Gym 100886J Sockets

J - Sockets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the

code forces 140F New Year Snowflake

F. New Year Snowflake As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of t

Code Forces 414B 很不错的双手,以促进合规

http://codeforces.com/problemset/problem/414/B 题目挺不错的.留个纪念,活动脑筋不错的题目 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack>

code forces 148D Bag of mice (概率DP)

time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies

code forces 979C

C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n?1n?1 bidirectio

code forces 990C

http://codeforces.com/contest/990/problem/C C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A bracket sequence is a string containing only charac

code forces 994C

C. Two Squares time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coo