codeforces1251c C. Minimize The Integer(贪心)

You are given a huge integer ? consisting of ? digits (? is between 1 and 3⋅105, 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 2).

For example, if ?=032867235 you can get the following integers in a single operation:

302867235 if you swap the first and the second digits;
023867235 if you swap the second and the third digits;
032876235 if you swap the fifth and the sixth digits;
032862735 if you swap the sixth and the seventh digits;
032867325 if you swap the seventh and the eighth digits.
Note, that you can’t swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can’t swap digits on positions 3 and 4 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 ? (1≤?≤104) — the number of test cases in the input.

The only line of each test case contains the integer ?, its length ? is between 1 and 3⋅105, inclusive.

It is guaranteed that the sum of all values ? does not exceed 3⋅105.

Output
For each test case print line — the minimum integer you can obtain.

Example
inputCopy
3
0709
1337
246432
outputCopy
0079
1337
234642
Note
In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 070⎯⎯⎯⎯9→0079.

In the second test case, the initial integer is optimal.

In the third test case you can perform the following sequence of operations: 24643⎯⎯⎯⎯2→2463⎯⎯⎯⎯42→243⎯⎯⎯⎯642→234642.

题意: 可以交换相邻且奇偶性不同的两个数。若干次这样的操作所能得到的最小值是什么
思路; 奇数移到一坨,偶数移到一坨。每次去两坨数最小的那个就可以了。

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
 
using namespace std;
 
char a[300005];
vector<int>num0,num1;
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        num0.clear();num1.clear();
        scanf("%s",a + 1);
        int len = (int)strlen(a + 1);
        int last = 1;
        for(int i = 1;i <= len;i++)
        {
            if((a[i] - '0') % 2 == 0)num0.push_back(a[i] - '0');
            else num1.push_back(a[i] - '0');
        }
        int len0 = num0.size(),len1 = num1.size();
        int cnt0 = 0,cnt1 = 0;
        while(cnt0 < len0 && cnt1 < len1)
        {
            if(num0[cnt0] > num1[cnt1])
            {
                printf("%d",num1[cnt1]);
                cnt1++;
            }
            else
            {
                printf("%d",num0[cnt0]);
                cnt0++;
            }
        }
        for(int i = cnt0;i < len0;i++)
            printf("%d",num0[i]);
        for(int i = cnt1;i < len1;i++)
            printf("%d",num1[i]);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值