题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
int main() {
string s;
getline(cin,s);
int len = s.size();
vector<char> res(len);
vector<vector<char>> ve(26);
vector<int> oth;
for(int i=0; i<len; i++){
if((s[i] >= 'A' && s[i] <='Z') || (s[i] >= 'a' && s[i] <='z')){
char tmp = tolower(s[i]);
ve[static_cast<int>(tmp-'a')].push_back(s[i]);
// cout << s[i];
}else{
// cout << s[i];
res[i] = s[i];
oth.push_back(i);
}
}
int num = 0, other = 0, o_len = oth.size();
for(int i=0; i<26; i++){
int ll = ve[i].size();
for(int j=0; j<ll; j++){
if(other < o_len && num == oth[other]){
other++;
j--;
}else{
res[num] = ve[i][j];
}
num++;
}
}
for(auto& i:res){
cout << i;
}
cout << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
这里要注意特殊情况:即输入字符串中全是字母的情况
此时需要规范数组的范围,要不然程序会崩溃。

查看12道真题和解析