题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 1. 先把每个字符出现的次数放在vector数组中
// 2. 对vector数组进行排序
// 3. 遍历数组,同时累加每个字符的漂亮权重,计算漂亮度
int main() {
int n;
cin >> n;
string str;
for(int i = 0; i < n; i++){
cin >> str;
vector<int> cnt(26, 0);
for(auto s : str){
cnt[s - 'a'] += 1;
}
sort(cnt.begin(), cnt.end());
int start = 1;
int beauty = 0;
for(auto n : cnt){
beauty += start*n;
start++;
}
cout << beauty << endl;
}
}
// 64 位输出请用 printf("%lld")
字符串操作 文章被收录于专栏
字符串操作

