题解 | #HJ45名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
#include <bits/stdc++.h>
#include<cctype>
using namespace std;
struct node {
char name;
int times = 0;
};
bool cmp(node a, node b) {
return a.times > b.times;
}
int main() {
int n;
cin >> n;
while (n--) {
vector<node> v(26);
vector<int> value(26);
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (isupper(s[i])) s[i] += 32;
v[s[i] - 'a'].name = s[i];
v[s[i] - 'a'].times += 1;
}
sort(v.begin(), v.end(), cmp);
int temp = 26;
for (int i = 0; i < v.size(); i++) {
if (v[i].times > 0) value[v[i].name - 'a'] = temp--;
}
int ans = 0;
for (int i = 0; i < s.size(); i++) ans += value[s[i] - 'a'];
cout << ans << endl;
}
}
// 64 位输出请用 printf("%lld")