[codefroces 1251c]Minimize The Integer

奇数相对位置不变,偶数相对位置不变。把他们放两个栈里。每次从两个栈顶选最小的一个。

    #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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值