codeforces 101A HOME 排序



Description

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 ofn 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 thank characters, it will be very suspicious.

Find the least number of distinct characters that can remain in the string after no more thank 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 numberk (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 thank characters.

Print on the second line the string that Gerald can get after some characters are lost. The string should have exactlym distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactlym distinct characters, print any of them.

Sample Input

Input
aaaaa
4
Output
1
aaaaa
Input
abacaba
4
Output
1
aaaa
Input
abcdefgh
10
Output
0

Sample Output

Hint

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的所有字符按原来的字符顺序输出;

AC代码:

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

struct point {
    int sum;
    char c;
};
point po[27];
char a[100010];
int k;
int tol;

int cmpsum(point a,point b){
    return a.sum<b.sum;
}
int main(){
    while(~scanf("%s",a)){
            scanf("%d",&k);
            memset(po,0,sizeof(po));
        int len=(int)strlen(a);
        int le=len;
        for(len=len-1;len>=0;len--){
            po[a[len]-'a'].sum++;
            po[a[len]-'a'].c= a[len];
        }//统计
        sort(po,po+26,cmpsum);//排序
        tol=0;
        int i;
        for(i=0;i<26;i++){
            if(po[i].sum==0){continue;}
            tol+=po[i].sum;
            if(tol<=k){
                for(int j=0;j<le;j++){
                    if(a[j]==po[i].c){
                        a[j]='0';
                    }
                }
            }
            if(tol>k){break;}
        }//删除字符
        printf("%d\n",26-i);
        for( int j=0;j<le;j++){
                if(a[j]!='0'){
                 printf("%c",a[j]);
                }
        }
        printf("\n");
    }
    return 0;
}

简直是炸了,比赛的时候连题都读不懂,更别说做了,英语渣的难处啊,,,,,,,,,,,,,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值