题目详情
我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同,
而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度是一样的。
现在给定一个字符串,输出它的最大可能的完美度。
例如:dad,你可以将26分配给d,25分配给a,这样整个字符串最大可能的完美度为77。
/*********************************
* 日期:2013-11-03
* 作者:SJF0115
* 题号: 题目 字符串的完美度
* 来源:http://hero.pongo.cn/Question/Details?ID=71&ExamID=69
* 结果:AC
* 来源:庞果网
* 总结:
**********************************/
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int cmp(const void*a,const void*b)
{
return *(int*)b - *(int*)a;
}
int perfect(const string &s){
int i;
int len = s.size();
int count[26] = {0};
//统计字母个数
for(i = 0;i < len;i++){
if(s[i] >= 'A' && s[i] <= 'Z'){
count[s[i] - 'A'] ++;
}
else if(s[i] >= 'a' && s[i] <= 'z'){
count[s[i] - 'a'] ++;
}
}
//从大到小排序
qsort(count,26,sizeof(count[0]),cmp);
int perfectDegree = 26;
int sum = 0;
//计算完美度
for(i = 0;i < 26;i++){
sum += count[i] * perfectDegree;
perfectDegree --;
}
return sum;
}
int main()
{
int i,n;
string str;
while(cin>>str){
printf("%d\n",perfect(str));
}
return 0;
}
天猫搞活动。。。。。。。。
我在用来往,用它我们就能免费语聊!11月1日到11月9日用淘宝账号登陆我还能送你2元双11现金红包!点击
点击打开链接 ,赶紧拿红包吧!