题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/d9aa3894d3aa4887843a85d26daa4437
//设置大小写标记,然后稳定排序
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
#pragma warning(disable:4996)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define repr(i, a, b) for(int i = a; i >= (b); --i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
const int maxn = 10005;
const int INF = 0x3f3f3f3f;
struct node {
char ch;
bool isbig;
node (char c, bool is) : ch(c), isbig(is) {}
};
bool cmp (node a, node b) {
return a.ch < b.ch;
}
int main() {
string str;
while (getline(cin, str))
{
vector<node> t;
rep(i, 0, sz(str)) {
if (str[i] >= 'a' && str[i] <= 'z')
t.push_back(node(str[i], false));
else if (str[i] >= 'A' && str[i] <= 'Z')
t.push_back(node(str[i] - 'A' + 'a', true));
}
stable_sort(t.begin(), t.end(), cmp);
int idx = 0;
rep(i, 0, sz(str)) {
if (isalpha(str[i])) {
if (t[idx].isbig)
cout << (char)(t[idx++].ch - 'a' + 'A');
else
cout << (char)(t[idx++].ch);
}
else
cout << str[i];
}
cout << endl;
}
return 0;
}