306785: CF1251C. Minimize The Integer

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Minimize The Integer

题意翻译

给你一个大整数$a$,它由$n$位数字组成($n$在$1$到$3 \times 10 ^ 5$之间)。它有可能会包含前导零。 你可以交换相邻位置的两个数字,如果这两个数字奇偶性不同(换言之,它们被二除的余数不同)。 举例来说:如果$a = 032867235$,你可以通过一次操作得到下面的整数: - $302867235$ 如果你交换第一个和第二个数字 - $023867235$ 如果你交换第二个和第三个数字 - $032876235$ 如果你交换第五个和第六个数字 - $032862735$ 如果你交换第六个和第七个数字 - $032867325$ 如果你交换第七个和第八个数字 注意:你不能更换第二个和第四个数字,因为他们不相邻。你也不能更换第三个和第四个数字因为他们的奇偶性相同。 你可以做任意次操作(当然也可能不做)。 请你找到通过这种操作可以得到的最小整数。 注意答案也可以有前导零。 ------ **简洁题面:** 给你一个大整数$a$,它由$n$位数字,也可能有前导零。现在给你一种操作规则:如果相邻的两个数字的奇偶性不同,那么你就可以交换它们。现在可以做任意次操作(可能一次都不做),求出通过这些操作可以获得的最小整数是多少。(答案可以包含前导零)

题目描述

You are given a huge integer $ a $ consisting of $ n $ digits ( $ n $ is between $ 1 $ and $ 3 \cdot 10^5 $ , 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 $ a = 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.

输入输出格式

输入格式


The first line contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — 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 $ and $ 3 \cdot 10^5 $ , inclusive. It is guaranteed that the sum of all values $ n $ does not exceed $ 3 \cdot 10^5 $ .

输出格式


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

输入输出样例

输入样例 #1

3
0709
1337
246432

输出样例 #1

0079
1337
234642

说明

In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $ 0 \underline{\textbf{70}} 9 \rightarrow 0079 $ . In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: $ 246 \underline{\textbf{43}} 2 \rightarrow 24 \underline{\textbf{63}}42 \rightarrow 2 \underline{\textbf{43}} 642 \rightarrow 234642 $ .

Input

题意翻译

给你一个大整数$a$,它由$n$位数字组成($n$在$1$到$3 \times 10 ^ 5$之间)。它有可能会包含前导零。 你可以交换相邻位置的两个数字,如果这两个数字奇偶性不同(换言之,它们被二除的余数不同)。 举例来说:如果$a = 032867235$,你可以通过一次操作得到下面的整数: - $302867235$ 如果你交换第一个和第二个数字 - $023867235$ 如果你交换第二个和第三个数字 - $032876235$ 如果你交换第五个和第六个数字 - $032862735$ 如果你交换第六个和第七个数字 - $032867325$ 如果你交换第七个和第八个数字 注意:你不能更换第二个和第四个数字,因为他们不相邻。你也不能更换第三个和第四个数字因为他们的奇偶性相同。 你可以做任意次操作(当然也可能不做)。 请你找到通过这种操作可以得到的最小整数。 注意答案也可以有前导零。 ------ **简洁题面:** 给你一个大整数$a$,它由$n$位数字,也可能有前导零。现在给你一种操作规则:如果相邻的两个数字的奇偶性不同,那么你就可以交换它们。现在可以做任意次操作(可能一次都不做),求出通过这些操作可以获得的最小整数是多少。(答案可以包含前导零)

加入题单

上一题 下一题 算法标签: