题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool cmp(int a, int b) { return a > b; } int beaut(string str) { int arr[26] = { 0 }; for (int i = 0; i < str.size(); i++) { arr[str[i] - 'a'] += 1; } vector<int>vec; vec.clear(); for (int i = 0; i < 26; i++) { if (arr[i] != 0) { vec.push_back(arr[i]); } } sort(vec.begin(), vec.end(), cmp); vector<int>::iterator it; int quanzhi = 26; int res = 0; for (it = vec.begin(); it != vec.end(); it++) { int tmp = (*it) * quanzhi; //cout << "个数统计:" << (*it) << " " << "权值:" << quanzhi << endl; res += tmp; quanzhi--; } return res; } int main() { //思路: //首先对字符重复个数进行统计; //然后对字符个数进行排序; //最后按照26 ....递减赋值进行统计; int n; cin >> n; string input; vector<string>input_string; for (int i = 0; i < n; i++) { cin >> input; input_string.push_back(input); } for (int i = 0; i < input_string.size(); i++) { cout << beaut(input_string[i]) << endl; } return 0; }