题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/d9aa3894d3aa4887843a85d26daa4437
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct node{ char c; int value; int index; bool operator< (const node &t) const{ if(value!=t.value) return value<t.value; else return index<t.index; } }; int main() { string str; while(getline(cin,str)){ vector<node> a; for(int i=0;i<str.size();i++){ char c=str[i]; if(isalpha(c)) a.push_back({c,tolower(c)-'a',i}); } sort(a.begin(),a.end()); int j=0; for(int i=0;i<str.size();i++){ char c=str[i]; if(isalpha(c)) cout<<a[j++].c; else cout<<c; } cout<<endl; } return 0; }