C. Minimize The Integer

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given a huge integer a consisting of n digits (n is between 1 and 3⋅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 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.

Input

The first line contains one integer t (1≤t≤104) — 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⋅105, 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.

Example

inputCopy

3

0709

1337

246432

outputCopy

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→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→234642.

因为只能交换奇数偶数,所以奇数偶数相对位置不变。然后将奇偶数分成两个队列,比较大小输出完事了!

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=300010;
typedef long long ll;
vector<int>v1,v2; int t;
char s[maxn];
int main()
{
scanf("%d",&t);
while(t--){
v1.clear();
v2.clear();
scanf("%s",s);
int len=strlen(s);
int x;
for(int i=0;i<len;i++){
x=s[i]-'0';
if(x%2)v1.push_back(x);
else v2.push_back(x);
}
int i,j;
for( i=0,j=0;i<v1.size()&&j<v2.size();){
if(v1[i]<v2[j]) cout<<v1[i],i++;
else cout<<v2[j],j++;
}
if(i<v1.size()) for(int k=i;k<v1.size();k++)cout<<v1[k];
if(j<v2.size()) for(int k=j;k<v2.size();k++)cout<<v2[k];
cout<<endl;
}
}

Codeforce 1251C. Minimize The Integer的更多相关文章

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

    链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...

  2. Educational Codeforces Round 75

    目录 Contest Info Solutions A. Broken Keyboard B. Binary Palindromes C. Minimize The Integer D. Salary ...

  3. ATC/TC/CF

    10.25 去打 CF,然后被 CF 打了. CF EDU 75 A. Broken Keyboard 精神恍惚,WA 了一发. B. Binary Palindromes 比赛中的憨憨做法,考虑一个 ...

  4. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  5. SGU 248. Integer Linear Programming( 背包dp )

    看了半天...发现就是个背包...然后就不打算敲了. 看了一眼forum..顿时吓傻..其他人用了gcd啊什么的各种奇怪的东西..然后还是敲了个背包结果就AC了= =既然写了代码就扔上来吧... -- ...

  6. Minimize the error CodeForces - 960B

    You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined  ...

  7. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

  8. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

  9. Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心

    B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...

随机推荐

  1. 家庭记账本app进度之android中AlertDialog的相关应用以及对日期时间的相关操作(应用alertdialog使用的谈话框)

    对于AlertDialog的相关知识: 1.创建构造器AlertDialog.Builder的对象:    2.通过构造器对象调用setTitle.setMessage.setIcon等方法构造对话框 ...

  2. C语言折半查找法练习题冒泡排序

    C语言折半查找法练习题 折半查找法: 折半查找法是效率较高的一种查找方法.假设有已经按照从小到大的顺序排列好的五个整数num[0]~num[4],要查找的数是key,其基本思想是: 设查找数据的范围下 ...

  3. python批量添加hexo文章封面

    ❝ 本文需要工具: 「excel」 「python3.x」 ❞ 今天突然觉得,我的博客的文章更新这么多了竟然还没有一个封面,觉得首页相当低调了- 首页 正好皮肤带有文章封面功能,所以我觉得要将文章批量 ...

  4. redis list 基本操作

    写在前面的话 本篇笔记写在笔者刚工作时.如有问题,请指教. 简介 list是链表,redis list的应用场景很多,也是Redis 最重要的数据结构之一,比如微博的关注列表,粉丝列表,消息列表等功能 ...

  5. 分布式 and 集群

    集群是个物理形态,强调个体和群体之间的联系: 同一个业务部署在多个服务器上,形成的逻辑上的整体. 分布式是个工作方式.强调请求和处理直接的分发状况: 一个业务分拆多个子业务,部署在不同的服务器上,通过 ...

  6. PDF阅读器

    1.SumatraPDF 非常小巧,打开很轻快 2.PDF Reader by Xodo window商店中可以找到,很好用 3. PDFXChange Editor这是我迄今为止见过的最好的PDF编 ...

  7. commonJS、AMD和CMD之间的区别

    JS中的模块规范(CommonJS,AMD,CMD),如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已. 现在就看看吧, ...

  8. Daily Scrum 1/18/2016

    Yandong & Zhaoyang: Prepare bug bash slides for Beta release; Dong & Fuchen:Prepare demo for ...

  9. [书籍精读]《React Native精解与实战》精读笔记分享

    写在前面 书籍介绍:本书由架构师撰写,包含ReactNative框架底层原理,以及与iOS.Android混合开发案例,精选了大量实例代码,方便读者快速学习.主要内容分为两大部分,第1部分" ...

  10. Spring Cloud+nacos+Feign,实现注册中心及配置中心

    写在前面 注册中心.配置中心的概念就不在这里解释了.发现服务原来一直用的是Eureka,因为这家伙闭源了,不爽.然后就发现了nacos,阿里巴巴的,好东西,一个搞定注册中心和配置中心.官网:https ...