Complexity(水题)

Description

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.

Sample Input

string 

Sample Output

4

Sample Input

letter 

 

Sample Output

2

Sample Input

aaaaaa

Sample Output

0

Sample Input

uncopyrightable 

Sample Output

13

Sample Input

ambidextrously 

Sample Output

12

Sample Input

assesses

Sample Output

1

 

Sample Input

assassins 

Sample Output

2

题解:就是记录每个字母出现次数,用最少去除字母的步骤使字符串的复杂度小于等于2。

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int main()
{
    int m,n,sum=0,s[1111111];
    char a[1111111];
    cin>>a;
    int l=strlen(a);
    sort(a,a+l);
    s[0]=1;
    int k=0;
    for(int i=1; i<l; i++)
        if(a[i]==a[i-1])
            s[k]++;
        else
            s[++k]=1;
	sort(s,s+k+1);
	int j=0;
    if(k<2)
        cout<<"0"<<endl;
    else
	{
		while(k>=2)
        {
        	sum+=s[j++];
        	k--;
        }
        cout<<sum<<endl;
	}
    return 0;
}


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值