我是靠谱客的博主 朴素犀牛,最近开发中收集的这篇文章主要介绍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;
}

 

转载于:https://www.cnblogs.com/l609929321/p/9215179.html

最后

以上就是朴素犀牛为你收集整理的CF999C Alphabetic Removals 思维 第六道 水题的全部内容,希望文章能够帮你解决CF999C Alphabetic Removals 思维 第六道 水题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部