C. Minimize The Integer(Educational Codeforces Round 75 (Rated for Div. 2))

C. Minimize The Integer(Educational Codeforces Round 75 (Rated for Div. 2))

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Description

You are given a huge integer a a a consisting of n n n digits ( n n n is between 1 1 1 and 3 ⋅ 1 0 5 3⋅10^5 3105, 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 2 2).

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

  • 302867235 302867235 302867235 if you swap the first and the second digits;
  • 023867235 023867235 023867235 if you swap the second and the third digits;
  • 032876235 032876235 032876235 if you swap the fifth and the sixth digits;
  • 032862735 032862735 032862735 if you swap the sixth and the seventh digits;
  • 032867325 032867325 032867325 if you swap the seventh and the eighth digits.

Note, that you can’t swap digits on positions 2 2 2 and 4 4 4 because the positions are not adjacent. Also, you can’t swap digits on positions 3 3 3 and 4 4 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 t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases in the input.

The only line of each test case contains the integer a, its length n is between 1 1 1 and 3 ⋅ 1 0 5 3⋅10^5 3105, inclusive.

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

Output

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

input

3
0709
1337
246432

output

0079
1337
234642

Note

In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0709 → 0079 0709→0079 07090079.

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

In the third test case you can perform the following sequence of operations: 246432 → 246342 → 243642 → 234642 246432→246342→243642→234642 246432246342243642234642.

题解

我就纳闷为什么当时不会写。。。

一个奇数如果旁边有偶数的话,它就可以和旁边的偶数换位置,偶数亦然,那么如果一个队列里只有一个奇数,剩下的全是偶数的话奇数就可以出现在任何它想去的位置。也就是说奇数和偶数之间的相对位置是可以随意设置的,但是奇数不能和奇数换位置,那么奇数之间的相对位置就是固定的,偶数亦然。

那么把序列里的奇数和偶数分离开,然后把两个序列按字典序最小合并到一个序列里就是答案了。

代码

#include <iostream>
#include <algorithm>
#include <vector>
#define maxn 300005
#define _for(i, a) for(int i = 0; i < (a); i++)
using namespace std;
const int inf = 0x3f3f3f3f;

const double eps = 1e-8;

char a[maxn];
vector<int>b[2];
char ans[maxn * 2];
int al;

void init() {
	_for(i, 2) b[i].clear();
	al = 0;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	int n;
	cin >> n;
	_for(q, n) {
		init();
		cin >> a;
		for (int i = 0; a[i]; i++) {
			int x = a[i] - '0';
			b[x % 2].push_back(x);
		}
		vector<int>::iterator i = b[0].begin(), j = b[1].begin();
		while (i != b[0].end() || j != b[1].end()) {
			int tem = inf, f = -1;
			if (i != b[0].end() && *i < tem) tem = *i, f = 0;
			if (j != b[1].end() && *j < tem) tem = *j, f = 1;
			ans[al++] = tem + '0';
			(f ? j : i)++;
		}
		ans[al++] = '\0';
		cout << ans << "\n";
	}
	return 0;
}

/*

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值