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