知识点与难度
字符串、贪心、中等难度
描述
给出一个字符串,该字符串仅由小写字母组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个不同字母拥有相同的“漂亮度”。字母忽略大小写。
给出多个字符串,计算每个字符串最大可能的“漂亮度”。
本题含有多组数据。
数据范围:输入的名字长度满足1≤n≤10000
输入描述:
第一行一个整数N,接下来N行每行一个字符串
输出描述:
每个字符串可能的最大漂亮程度
输入输出示例:
输入:
2
zhangsan
lisi
输出:
192
101
我的代码:
#include <functional>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s="";
int ch[26] = {0};
cin >> s;
for (int j = 0; j < s.length(); j++) {
ch[s[j] - 'a'] = ch[s[j] - 'a'] + 1;
}
sort(ch, ch + 26,greater<int>());
int count=0;
int k = 26;
for (int j = 0; j <= 26 && ch[j] != 0; j++) {
count += k * ch[j];
k--;
}
cout << count << endl;
}
}
// 64 位输出请用 printf("%lld")
我的代码分析:
我这里创建了一个数据用来记录各个字母的频次,出现一次对应频次+1,最后排一下序,从大到小乘法加和输出。这道题知识点写的贪心,不过我没用到。
top1代码:
#include <iostream>
using namespace std;
int main(){
cout<<"hello world!"<<endl;
}
top1代码分析:
无
总结:
无