题解 | #名字的漂亮度# C++ 桶 + 排序
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; int getBeauty(string str) { int sum = 0; vector<int> alpha(26, 0); for (char c : str) { alpha[c - 'a']++; } sort(alpha.begin(), alpha.end(), greater<int>()); int plus = 26; for (int n : alpha) { int num = n; while (num > 0) { sum += plus; num--; } plus--; } return sum; } int main() { int N; string str; cin >> N; for (int i = 0; i < N; ++i) { cin >> str; cout << getBeauty(str) << endl; } return 0; }