题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
using namespace std;
int main(){
string str;
getline(cin, str);
map<char, queue<char>> map;
for(int i = 0; i < str.size(); i++) {
if(isupper(str[i])) { //大写字母
map[str[i]].push(str[i]);
}
else if(islower(str[i])) {//小写字母
map[toupper(str[i])].push(str[i]);
}
else{
}
}
auto it = map.begin();
for(int i = 0; i < str.size(); i++) {
if(isupper(str[i]) || islower(str[i])) {
str[i] = it ->second.front();
it ->second.pop();
if(it -> second.empty()) {
it++;
}
}
}
cout << str;
}
#刷题#
#include <string>
#include <vector>
#include <map>
#include <queue>
using namespace std;
int main(){
string str;
getline(cin, str);
map<char, queue<char>> map;
for(int i = 0; i < str.size(); i++) {
if(isupper(str[i])) { //大写字母
map[str[i]].push(str[i]);
}
else if(islower(str[i])) {//小写字母
map[toupper(str[i])].push(str[i]);
}
else{
}
}
auto it = map.begin();
for(int i = 0; i < str.size(); i++) {
if(isupper(str[i]) || islower(str[i])) {
str[i] = it ->second.front();
it ->second.pop();
if(it -> second.empty()) {
it++;
}
}
}
cout << str;
}
#刷题#