zoukankan      html  css  js  c++  java
  • 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 ≤ 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.

    Sample test(s)
    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.

    http://codeforces.com/problemset/problem/101/A

    //蛋疼的英语呀

    //去问了下华东交大的朋友这个题目的输出要求

    //然后下面的程明明童鞋的代码

    //真是简洁、厉害呀、直接导致结果我下面写的代码几乎和他一样了

    //要是我自己写的话、我估计我会有好些判断语句之类的、、

    //解法、贪心、每次删掉出现次数最少的

    #include <iostream>
    #include <algorithm>
    #include <string>

    using namespace std;

    string S;
    int K,cnt[26],id[26],flag[26],result;

    bool cmp(int x,int y)
    {
        return cnt[x]<cnt[y];
    }

    int main()
    {
        cin >> S >> K;
        for (int i=0;i<S.size();i++)
            cnt[S[i]-'a']++;
        for (int i=0;i<26;i++)
            id[i]=i;
        sort(id,id+26,cmp);
        for (int i=0;i<26;i++)//精辟呀,直接把没出现的字母当做出现次数为0
        {                           
            if (K<cnt[id[i]])
                break;
            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<S.size();i++)
            if (!flag[S[i]-'a'])
                cout << S[i];
        return 0;
    }

    //O(∩_∩)O~

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    char s[100002];
    struct node
    {
        int index,cnt;
        bool operator<(const node&a)const
        {
            return cnt<a.cnt;
        }
    };
    node r[26];
    bool flag[26];
    int main()
    {
        int K,cnt=0;
        int i;
        scanf("%s%d",s,&K);//只有一组数据、真好呀

        for(i=0;s[i]!='\0';i++)
          r[s[i]-'a'].cnt++;
        for(i=0;i<=26;i++)
         r[i].index=i;
         sort(r,r+26);
         for(i=0;i<26;i++)
         {
             if(K<r[i].cnt)
               break;
             K-=r[i].cnt;
             flag[r[i].index]=1;
         }
         for(i=0;i<26;i++)
          if(!flag[i])
            cnt++;
        printf("%d\n",cnt);
        for(i=0;s[i]!='\0';i++)
         if(!flag[s[i]-'a'])
          printf("%c",s[i]);
        printf("\n");
        return 0;
    }

  • 相关阅读:
    升级MySQL5.7.22版本_总结记录
    初探分布式环境的指挥官ZooKeeper
    利用ROS工具从bag包中提取图片和.csv文件
    安装tensorflow出现的python-setuptools 20.7.0问题
    evo 评测工具修改背景颜色和线条等参数
    Ubuntu上下载百度网盘资料
    okvis 编译出现ceres-solver错误的解决办法
    opencv各个模块功能总结
    计算两幅图的单应矩阵,实现图像拼接
    特征提取与匹配、基础矩阵、单应矩阵、极限约束
  • 原文地址:https://www.cnblogs.com/372465774y/p/2618963.html
Copyright © 2011-2022 走看看