Minimize The Integer(贪心)

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.

input:

3
0709
1337
246432

output:

0079
1337
234642

题意:给出一串数字,通过无限次交换数字位置,使得最后的数字最小。且每次交换只能奇数与偶数交换。最后数字可以有前导0.

解题思路:只有奇数和偶数可以交换,那么奇数与奇数之间的相对位置是不会改变的(偶数与偶数同理)。

因此把奇数和偶数分别存在两个队列中,逐一比较队头元素的大小,小者出队并输出即可。

核心代码:

int q1[N],q2[N],h1,h2,t1,t2;
void solve(){
	string s; cin >> s;
	h1 = h2 = 0;t1 = t2 = -1;
	for(int i = 0;i < s.size();i++){
		if((s[i] - '0') & 1){
			q1[++t1] = s[i] - '0';
		}else{
			q2[++t2] = s[i] - '0';
		}
	}
	while(h1 <= t1 && h2 <= t2){
		if(q1[h1] < q2[h2]){
			cout<< q1[h1++];
		}else{
			cout << q2[h2++];
		}
	}
	while(h1 <= t1) cout << q1[h1++];
	while(h2 <= t2) cout << q2[h2++];
	
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值