Codeforces 101A Homework(贪心)

题目链接

A. Homework

time limit per test    2 seconds

memory limit per test    256 megabytes

input    standard input

output    standard output

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 ≤ 10^5). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 10^5).

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,可以删除最多k个字符,问删除后的字符串中字符的最少种类数m以及删除后的子字符串。

解题思路:

将字符种类的数目按升序排列,在k的范围内尽量多的删除字符种类。

AC代码:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
using namespace std;
struct node
{
	int num;//该字符的数目 
	char ch;//该字符的种类 
	node(int n=0,char c=' '){num=n;ch=c;}
	friend bool operator <(node a,node b)//按数目排序 
	{
		return a.num<b.num;
	}
}low[27];
bool isdle[27];//isdle[i]为true表示字符char(i+'a'-1)被删除 
int main()
{
	int n;
	string str;
	cin>>str>>n;
	memset(isdle,0,sizeof(isdle));
	for(int i=0;i<str.length();i++)
	{
		low[str[i]-'a'+1].num ++;
		low[str[i]-'a'+1].ch =str[i];
	}
	sort(low+1,low+26+1);//升序排列 
		
	int t=0;
	for(int i=1;i<=26;i++)
	{
		if(low[i].num&&t+low[i].num<=n)//在n的范围内尽可能的删除字符种类 
		{
			isdle[low[i].ch-'a'+1]=1;
			t+=low[i].num;
		}
		else if(t+low[i].num>n) break;
	}
	
	int m=0;
	for(int i=1;i<=26;i++)
	if(low[i].num&&!isdle[low[i].ch-'a'+1]) m++;
	cout<<m<<endl;//删除后子字符串的字符最少种类数 
	
	for(int i=0;i<str.length();i++)//输出子字符串时忽略掉要删除的字符 
		if(!isdle[str[i]-'a'+1]) cout<<str[i];
	
}

附上一段超时代码,引以为戒:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
using namespace std;
struct node
{
	int num;//该字符的数目 
	bool isdle;//该字符是否被删除 
	char ch;
	node(bool is=0,int n=0,char c=' '){isdle=is;num=n;ch=c;}
	friend bool operator <(node a,node b)
	{
		return a.num<b.num;
	}
}low[27];
int main()
{
	int n;
	string str;
	cin>>str>>n;
	
	for(int i=0;i<str.length();i++)
	{
		low[str[i]-'a'+1].num ++;
		low[str[i]-'a'+1].ch =str[i];
	}
	sort(low+1,low+26+1);
		
	int t=0;
	for(int i=1;i<=26;i++)
	{
		if(low[i].num&&t+low[i].num<=n)
		{
			low[i].isdle=1;
			t+=low[i].num;
		}
		else if(t+low[i].num>t) break;
	}
	
	int m=0;
	for(int i=1;i<=26;i++)
	if(low[i].num&&!low[i].isdle) m++;
	
	for(int i=1;i<=26;i++)
	{
		if(low[i].num&&low[i].isdle)
		{
			string p;p+=low[i].ch;
			for(int j=1;j<=low[i].num;j++)
			str.erase(str.find(p,0),1);
		}
	}
	
	cout<<m<<endl;
	if(str.length()) cout<<str;
}

猜你喜欢

转载自blog.csdn.net/qq_40889820/article/details/82223930