A13023 | Minimize The Integer
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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$ .
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted