C. Minimize The Integer

194 篇文章 1 订阅

链接:https://codeforces.com/contest/1251/problem/C

You are given a huge integer aa consisting of nn digits (nn is between 11 and 3⋅1053⋅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 22).

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

  • 302867235302867235 if you swap the first and the second digits;
  • 023867235023867235 if you swap the second and the third digits;
  • 032876235032876235 if you swap the fifth and the sixth digits;
  • 032862735032862735 if you swap the sixth and the seventh digits;
  • 032867325032867325 if you swap the seventh and the eighth digits.

Note, that you can't swap digits on positions 22 and 44 because the positions are not adjacent. Also, you can't swap digits on positions 33 and 44 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 tt (1≤t≤1041≤t≤104) — the number of test cases in the input.

The only line of each test case contains the integer aa, its length nn is between 11 and 3⋅1053⋅105, inclusive.

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

Output

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

Example

input

Copy

3
0709
1337
246432

output

Copy

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→0079070_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→23464224643_2→2463_42→243_642→234642.

题解:

#include<bits/stdc++.h>
using namespace std;
int t;
char s[300001];
char a[300001],b[300001],ans[300001];
int main()
{
	cin>>t;
	while(t--)
    {
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(s,0,sizeof(s));
		memset(ans,0,sizeof(ans));
		cin>>s;
        int l=strlen(s);
        int m=0,k=0;
        for(int i=0;i<l;i++)
        {
            int x=s[i]-'0';
            if(x%2==0)
			{
				a[m]=s[i];
				m++;
			}
            else 
            {
            	b[k]=s[i];
            	k++;
            }
        }
        int i=0,j=0,u=0;
        while(i<m&&j<k)
        {
            if(a[i]<b[j])
			{
                ans[u]=a[i];
                i++;
            }
            else 
			{
                ans[u]=b[j];
                j++;
            }
            u++;
        }
        while(i<m)
		{
            ans[u]=a[i];
			i++;
			u++;
        }
        while(j<k)
		{
            ans[u]=b[j];
			u++;
			j++;
        }
        cout<<ans<<endl;
    }
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值