奇数相对位置不变,偶数相对位置不变。把他们放两个栈里。每次从两个栈顶选最小的一个。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
char s[400001],s1[400001],s2[400001];
int top1,top2;
//00
//79
//0709
//0
int main() {
int T;
scanf("%d",&T);
while(T--) {
top1 = top2 = 0;
scanf("%s",s);
int len = strlen(s);
for (int i = 0; i < len; i++)
if ((s[i]-'0')&1) s1[++top1] = s[i];
else s2[++top2] = s[i];
int x = 1,y = 1;
while (x<=top1 || y<=top2) {
if (x>top1) putchar(s2[y++]);
else if (y>top2) putchar(s1[x++]);
else if (s1[x]<s2[y]) putchar(s1[x++]);
else putchar(s2[y++]);
}
puts("");
}
return 0;
}
You are given a huge integer consisting of digits ( is between and
, inclusive). It may contain leading zeros.
You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by
).
For example, if
you can get the following integers in a single operation:
if you swap the first and the second digits;
if you swap the second and the third digits;
if you swap the fifth and the sixth digits;
if you swap the sixth and the seventh digits;
if you swap the seventh and the eighth digits.
Note, that you can’t swap digits on positions
and because the positions are not adjacent. Also, you can’t swap digits on positions and
because the digits have the same parity.
You can perform any number (possibly, zero) of such operations.
Find the minimum integer you can obtain.
Note that the resulting integer also may contain leading zeros.
Input
The first line contains one integer
(
) — the number of test cases in the input.
The only line of each test case contains the integer
, its length is between and
, inclusive
It is guaranteed that the sum of all values
does not exceed
.
Output
For each test case print line — the minimum integer you can obtain.