数组储存每个字母出现的次数,从大到小排序,依次乘以26,25,24,....,0,求和。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin>>n;
string str;
for( int i = 0; i<n;i++)
{
cin>>str;
int num[26] = {0};
int beauty = 0;
for(int j = 0; j<str.length(); j++)
num[str[j] - 'a'] += 1;
//排序
//while(prev_permutation(num, num+26)); //超时
for(int j = 0; j < 26-1; j++) //冒泡
for( int k = 0; k < 26-j-1; k++)
{
if(num[k] < num[k+1])
{
int tmp = num[k];
num[k] = num[k+1];
num[k+1] = tmp;
}
}
//输出漂亮度
for(int j = 0; j < 26; j++)
beauty += num[j] * (26-j);
cout << beauty << endl;
}
return 0;
}

京公网安备 11010502036488号