题解 | #字符串排序# cpp
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<char>> res(26); char c; int cnt=0; vector<pair<int,char>> mark; while((c=getchar())!='\n'){ cnt++; if(c>='A'&&c<='Z'){ res[c-'A'].push_back(c); }else if(c>='a'&&c<='z'){ res[c-'a'].push_back(c); }else{ mark.push_back(make_pair(cnt,c)); } } string s=""; int k=0; for(auto i:res){ for(auto j:i){ s+=j; } } if(mark.size()==0){ cout<<s; return 0; } for(int i=0;i<cnt;i++){ if(i+1==mark[k].first){ s.insert(i,1,mark[k].second); k++; } } cout<<s; } // 64 位输出请用 printf("%lld")
使用pair标记特殊字符的位置,最后插入