题解 | 字符串排序
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <cctype> #include <iostream> #include <vector> using namespace std; int main() { string s,str; char c; int i=0, len = 0; vector<string> vec(26); getline(cin, s); for(auto ch: s) { c = tolower(ch); if(c>='a' && c<='z') { vec[c-'a'] = ch + vec[c-'a']; } } for(auto ch:s) { c = tolower(ch); if(c>='a' && c<='z') { while(vec[i].empty()) //字符串空 { i++; } len = vec[i].size(); // 第i个字符串长度 cout << vec[i][len-1]; vec[i].erase(len-1, 1); } else { cout << ch; } } return 0; }