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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值