2015-2016 ACM-ICPC Complexity

题目地址:http://codeforces.com/gym/100819/attachments

题目:

Define the complexity of a string to be the number of distinct letters in it. For example, the stringstring has complexity 6 and the string letter has complexity 4.

You like strings which have complexity either 1 or 2. Your friend has given you a string andyou want to turn it into a string that you like. You have a magic eraser which will delete one letterfrom any string. Compute the minimum number of times you will need to use the eraser to turnthe string into a string with complexity at most 2.

Input

The input consists of a single line that contains a single string of at most 100 lowercase ASCIIletters (‘a’–‘z’).

Output

Print, on a single line, the minimum number of times you need to use the eraser.


思路:

没啥思路,水题。给你一堆字母,不管它有几种字母,要把它减少到只有两种,问最少删掉几个字母。

于是就用set统计个数,用数组记录每个字母的个数,贪心一下。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    string a;
    while(cin>>a)
    {
        set<char>s;
        int b[150];
        memset(b,0,sizeof(b));
        int c=a.length();
        int i;
        for(i=0;i<c;i++)
        {
            s.insert(a[i]);
            b[a[i]]++;
        }
        if(s.size()<=2)cout<<0<<endl;
        else
        {
            //int t=s.size()-2;
            sort(b,b+150,cmp);
            int sum=0;
            for(i=s.size();i>=2;i--)
            {
                sum=sum+b[i];
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zero_979/article/details/80738815