题解 | #名字的漂亮度#
名字的漂亮度
https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int N;
cin >> N;
string s;
vector<char> c;//用c存储s中的不同字符
vector<int> num;//用num存储c中字符出现的次数
while (cin >> s) {
auto it = s.begin();
c.push_back(*it);
num.push_back(count(s.begin(), s.end(), *it));
++it;
while (it != s.end()) {
if (find(c.begin(), c.end(), *it) == c.end()) {//如果*it不在c中
c.push_back(*it);
num.push_back(count(s.begin(), s.end(), *it));
}
++it;
}
int i, j;
for (i = 0; i < num.size() - 1; ++i) {//冒泡排序
for (j = num.size() - 1; j > i; --j) {
if (num[j - 1] < num[j]) {
swap(num[j - 1], num[j]);
swap(c[j - 1], c[j]);//好像不需要这个操作
}
}
}
int name_beauty = 0;
for (i = 0; i < c.size(); ++i) {
name_beauty += num[i] * (26 - i);//出现最多的字符赋26,依次类推
}
cout << name_beauty << endl;
c.clear();//记得把c和num清空
num.clear();
}
return 0;
}
看懂题目后本题就很简单了:一个“名字”中至多有26种字母,对出现次数最多的字母赋予最大的漂亮度26,并依次类推即可。

