Vjudge 4.12 省赛训练赛 A. 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 ≤ 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
Copy
aaaaa
4
output
Copy
1
aaaaa
input
Copy
abacaba
4
output
Copy
1
aaaa
input
Copy
abcdefgh
10
output
Copy
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.

题目最开始读不懂

其实就是先输入一长串字母,输入一个数n,就是要删掉n个字母

希望把不同的字母删的越多越好,留下尽量少的字母

要是删的比原本的多或等于,就等于零

否则输出最后留下几个不同的字母

然后把这几个字母输出(全部),不许该顺序,在这里wa了三次,真的多此一举,以此警戒

代码写的很乱,训练赛时乱打的


#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
	char letter;
	int total;
}c[100];
bool cmp(node x,node y)
{
	return x.total<y.total;
}
int main()
{
	char a[100002];
	gets(a);
	char b[100];
	int k1=1,k2=1,k3=1;
	for(int i='a';i<='z';i++)
	{
		b[k1++]=i;
		c[k2++].letter=i;
		c[k3++].total=0;
	}
	int l=strlen(a);
	for(int i=1;i<=26;i++)
	{
		for(int j=0;j<l;j++)
		{
			if(a[j]==b[i]) 
			{
				c[i].total++;
			}
		}
	}
	sort(c+1,c+27,cmp);
	int n;
	cin>>n;
	if(n>=l) cout<<"0"<<endl;
	else
	{
		int sum=0;
		int kk=1;
		while(sum<=n)
		{
			sum=sum+c[kk++].total;
		}
		for(int i=0;i<l;i++)
		{
			for(int j=1;j<kk-1;j++)
			{
				if(a[i]==c[j].letter) a[i]='0';
			}
		}
		int ssum=0;
		for(int i=1;i<kk-1;i++)
		{
			ssum=ssum+c[i].total;
			c[i].total=0;
		}
		if(ssum+c[kk-1].total==n)
		{
			for(int i=0;i<l;i++)
				if(a[i]==c[kk-1].letter) a[i]='0';
		}
		int allsum=0;
		for(int i=1;i<=26;i++)
		{
			if(c[i].total!=0) allsum++;
		}
		cout<<allsum<<endl;
		for(int i=0;i<l;i++)
		{
			if(a[i]!='0') cout<<a[i];
		}
		cout<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值