tj
2025-09-22 16:39:37
发布于:浙江
1阅读
0回复
0点赞
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct LetterCount {
char letter;
int count;
};
bool compare(const LetterCount& a, const LetterCount& b) {
if (a.count != b.count) {
return a.count > b.count;
}
return a.letter < b.letter;
}
int main() {
string input;
cin >> input;
vector<int> count(26, 0);
for (char c : input) {
count[c - 'a']++;
}
vector<LetterCount> letterCounts;
for (int i = 0; i < 26; i++) {
if (count[i] > 0) {
letterCounts.push_back({static_cast<char>('a' + i), count[i]});
}
}
sort(letterCounts.begin(), letterCounts.end(), compare);
for (size_t i = 0; i < letterCounts.size(); i++) {
if (i > 0) {
cout << " ";
}
cout << letterCounts[i].letter;
}
cout << endl;
return 0;
}
这里空空如也






有帮助,赞一个