题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#队列 + 数组队列,逻辑清晰 #include <iostream> #include <limits> #include <queue> #include <vector> using namespace std; int main() { string s; getline(cin,s); queue<int> q; vector<queue<char>> vec(26); for(int i=0;i<s.size();++i) { int num = s[i] - 'A'; int num1 = s[i] - 'a'; if(num >=0 && num <26) { q.emplace(i); vec[num].emplace(s[i]); } if(num1 >= 0 && num1 <26) { q.emplace(i); vec[num1].emplace(s[i]); } } while(!q.empty()) { for(int i=0;i<26;++i) { while(!vec[i].empty()) { int idx = q.front(); char c = vec[i].front(); q.pop(); vec[i].pop(); s[idx] = c; } } } cout << s; } // 64 位输出请用 printf("%lld")