#include <algorithm> #include <iostream> #include <string> #include <map> #include <vector> using namespace std; // 比较函数,用于稳定排序 bool comper(const pair<string, int>& word1, const pair<string, int>& word2) { return word1.second > word2.second; } int main() { string str; map<string, int> word_count; // 获取整行输入 getline(cin, str); str[0] += 32; size_t pos = 0, prev = 0; // 通过空格分割字符串 while ((pos = str.find(' ', prev)) != string::npos) { string word = str.substr(prev, pos - prev); if (!word.empty()) { word_count[word]++; } prev = pos + 1; } // 处理最后一个单词 string last_word = str.substr(prev, str.size() - 1 - prev); if (!last_word.empty()) { word_count[last_word]++; } // 将map中的元素拷贝到vector中 vector<pair<string, int>> sorted_words(word_count.begin(), word_count.end()); // 按照出现次数进行排序 stable_sort(sorted_words.begin(), sorted_words.end(), comper); // 输出排序结果 for (const auto& entry : sorted_words) { cout << entry.first << ":" << entry.second << endl; } return 0; }