全靠自己写不出来,参考别人的
#include <iostream> #include <bits/stdc++.h> using namespace std; char lowercase(char c) { if(c >=97 && c<=122) { return c-('a'-'A'); } return c; } void processing(string& str) { int n = str.size(); vector<char> ret(n, '\0'); map<char, string> dic; for(int i=0;i<n;i++) { char c = str[i]; if(isalpha(c)) { auto it = dic.find(lowercase(c)); if(it != dic.end()) { it->second.push_back(c); } else { string temp; temp.push_back(c); dic.emplace(lowercase(c), temp); } } else { ret[i] = c; } } int i = 0; for(auto item : dic) { for(char ch : item.second) { while(i<n) { if(ret[i] == '\0') { ret[i] = ch; break; } i++; } } } for(auto e : ret) { cout << e; } cout << endl; } int main() { string str; getline(cin, str); processing(str); }