Codeforces999C——Alphabetic Removals

You are given a string s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (k≤n) from the string s. Polycarp uses the following algorithm k 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 k times, thus removing exactly k characters.
Help Polycarp find the resulting string.
Input
The first line of input contains two integers n and k (1≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.
The second line contains the string s consisting of n lowercase Latin letters.
Output
Print the string that will be obtained from s after Polycarp removes exactly k letters using the above algorithm k times.
If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).
Examples
Input
15 3
cccaabababaccbc
Output
cccbbabaccbc
Input
15 9
cccaabababaccbc
Output
cccccc
Input
1 1
u
Output

这个也是看样例才理解诶 题目讲得也是很奇怪
题意就是从a到z这样删除 优先删除前面的
但是题目这句话remove the leftmost occurrence and stop the algorithm 我也是醉了

所以我的思路就是用数组记录每个字母出现的次数,然后循环看多少个字母要全部删除的

代码:

#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int MAXN=400050;
char s[MAXN];
map<char,int> alp;
int main(void){
    int n,k;
    scanf("%d%d",&n,&k);
    scanf("%s",s);
    for(int i=0;i<n;i++){
        alp[s[i]]++;
    }
    char res;
    int idx=0;
    for(int i=0;i<26;i++){
        k-=alp[(char)('a'+i)];
        if(k<=0){
            res=(char)('a'+i);
            idx=k+alp[res];
            break;
        }
    }
    //printf("%c %d\n",res,idx);
    int w=0;
    for(int i=0;i<n;i++){
        if(s[i]<res){
            continue;
        }
        else if(s[i]==res && w<idx){
            w++;
            continue;
        }
        else{
            printf("%c",s[i]);
        }
    }
    printf("\n");
    return 0;
}
全部评论

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
10-27 17:26
东北大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务