欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

有关CodeForces - 101A的一点思考

程序员文章站 2022-04-11 10:29:50
...

之前做过类似的题,但这次遇到又不会了。。。于是写过东西把他记录下来。

Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That’s why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.

Input
The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).
Output
Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters.
Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.
Examples
Input
aaaaa
4
Output
1
aaaaa
Input
abacaba
4
Output
1
aaaa
Input
abcdefgh
10
Output
0
Note
In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters “a” from 1 to 5 in length.
In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from “a” (as they are no more than four), which will result in the “aaaa” string.
In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.

题意:给一串字符,要求通过删去不多于k个字符使得留下的字符串中字符种类最少。输出留下的字符串和字符种类数。

分析:需要把字符串中字符的总类型数和相应的数量记录下来,然后按从小到大的顺序一一删去。普通的一个一维数组只能记录类型和数量,所以需要另一个数组记录数量顺序。输出时利用flag数组可以更轻松的判断某一字符是否已经被删去。
代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;

string a;
int k,cnt[26],id[26],flag[26],result;
	
bool cmp(int a,int b)
{
	return cnt[a]<cnt[b];
}

int main()
{
	cin>>a>>k;
	memset(cnt,0,26);
	memset(id,0,26);
	memset(flag,0,26);
	for(int i=0;i<a.size();i++)
		cnt[a[i]-'a']++;	//将字符转化为数字储存,记录类型和数量
	for(int i=0;i<26;i++)
		id[i]=i;
	sort(id,id+26,cmp);	//另辟数组记录顺序
	for(int i=0;i<26;i++)
	{
		if(k<cnt[id[i]])
			break;	//多删无益
		else
			k-=cnt[id[i]];
			flag[id[i]]=1;	//标记已被删尽
	}
	for(int i=0;i<26;i++)
		if(!flag[i])
			result++;	//遍历数组,记录类型
	cout<<result<<endl;
	for(int i=0;i<a.size();i++)
		if(!flag[a[i]-'a'])
			cout<<a[i];
	return 0;
}

写在最后:
英语水平太差劲了。。。看来以后要少用谷歌翻译。

另:有问题可以在评论区指出。
代码来源:https://www.cnblogs.com/372465774y/archive/2012/08/01/2618963.html