【CodeForces 1251C --- Minimize The Integer】-程序员宅基地

技术标签: ACM  Minimize The Integer  CodeForces 1251C  

【CodeForces 1251C --- Minimize The Integer】


Description

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.

Sample Input

3
0709
1337
246432

Sample 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): 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.

解题思路:

根据题意我们可以理解为相同奇偶性的数字顺序是不能变的,能变得只是不同奇偶性的数字。
所以我们可以通过两个队列来分别按顺序存储奇数和偶数,然后判断大小依次输出就行。

AC代码:

#include <iostream>
#include <string>
#include <queue>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

int main()
{
    
    SIS;
    string s;
    queue<char> q1,q2;
    int T;
    cin >> T;
    while(T--)
    {
    
        string ans;
        cin >> s;
        int len=s.size();
        for(int i=0;i<len;i++)
        {
    
            if(s[i]&1) q1.push(s[i]);
            else q2.push(s[i]);
        }
        while(!q1.empty() || !q2.empty())
        {
    
            if(q1.empty() || (!q2.empty() && q1.front()>q2.front()))
            {
    
                ans.push_back(q2.front());
                q2.pop();
            }
            else
            {
    
                ans.push_back(q1.front());
                q1.pop();
            }
        }
        cout << ans << endl;
    }
    return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41879343/article/details/102745652

智能推荐

BPI-Bit 开发板带有Xtensa 32位LX6双核处理器的嵌入式系统的ESP 32_xtensa lx6-程序员宅基地

文章浏览阅读1.7k次。BPI:bit(也称为BPI-bit,作为webduino:bit)是一个带有Xtensa 32位LX6双核处理器的嵌入式系统的ESP 32。支持Webduino、Arduino、微python以及Scratch X编程环境。BPI:一些开发板5厘米宽5厘米长,重约10 ~ 12克,除了以下20销金手指接口,内置一个25全彩LED照明矩阵,两个光敏电阻按钮开关温度传感电阻蜂鸣器和九轴传感器(三轴加速度三轴陀螺仪和三轴磁罗盘),空间配置如下:全色LED矩阵:4光敏传感器:36(左)39(A3右上)按钮_xtensa lx6

2017界面UI设计风格流行什么?(二)_ui界面设计风格线性化-程序员宅基地

文章浏览阅读373次。那么卡片化呢? 如果你的想法创意还停留在卡片阶段,那就落伍了,因为无框界面的发展过程中,卡片逐渐被淡化。卡片是否有存在的必要?用户更多的不会去关注,他们只注意来这里的最终功能所在,界面对他们来讲或许只是寻求信息的方式。没错,卡片化的由来有它一定的道理。几年前大家注意到显示屏的尺寸越来越不可预期,设计师也需要一种方式来让设计出的界面能够适应不同尺寸的屏幕。卡片刚好能够解决这一_ui界面设计风格线性化

CRC循环校验码的系统仿真及其应用_simulink modbus crc16 检验模块-程序员宅基地

文章浏览阅读2.7k次,点赞7次,收藏40次。CRC即循环冗余校验码,是数据通信领域中最常用的一种检错校验码,其特征是信息字段和校验字段的长度可以任意选定,循环冗余检查是一种数据传输检错功能,对数据进行多项式计算,并将得到的结果附在帧的后面,接收设备也执行类似的算法,以保证数据传输的正确性和完整性循环冗余码CRC检验技术广泛应用于测控及通信领域。CRC计算可以靠专用的硬件来实现,但是对于低成本的微控制器系统,在没有硬件支持下实现CRC检验,关键的问题就是如何通过软件来完成CRC计算,也就是CRC算法的问题。_simulink modbus crc16 检验模块

Food Buying(CF-1296B)_t 组数据,每组给出 1 个数 n,代表有 n 元钱,现在用这 n 元钱买东西,每买 10 元返-程序员宅基地

文章浏览阅读789次。Problem DescriptionMishka wants to buy some food in the nearby shop. Initially, he has s burles on his card.Mishka can perform the following operation any number of times (possibly, zero): choose..._t 组数据,每组给出 1 个数 n,代表有 n 元钱,现在用这 n 元钱买东西,每买 10 元返

JSP内置对象、表达式和标签及JSP解析原理-程序员宅基地

文章浏览阅读1k次。JSP的学习路线 什么是JSP?——JSP的背景和发展情况简介 为什么要用JSP?——普通HTML编程中,我们遇到的了哪些问题? JSP的语法特点简介 JSP内置对象 EL表达式 JSP的标签式语法(指令和动作) JSTL标签库 JSP的解析编译执行过程* 什么是JSP?1.Java Server Pages(JSP) is a technology thathelps software develop...

ESP32开发-LVGL动画显示_lv_snprintf-程序员宅基地

文章浏览阅读4.4k次。LVGL动画LVGL 支持动态效果,包括动态切换屏幕,组件动画效果等等。动画创建步骤以及API说明以官方demo中的动画创建为例 //定义动画变量 lv_anim_t a; //初始化动画变量 lv_anim_init(&a); //设置要进行动画处理的组件 lv_anim_set_var(&a, gauge); //设置动画功能 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t_lv_snprintf

随便推点

STM32F030 使用内部时钟和外部时钟_pll_source_hsi-程序员宅基地

文章浏览阅读1.2w次,点赞2次,收藏10次。static void SetSysClock(void){ __IO uint32_t StartUpCounter = 0, HSEStatus = 0; /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/#if defined (PLL_SOURCE_HSI) /..._pll_source_hsi

Android实现三级联动下拉框 下拉列表spinner_android省市县下拉框-程序员宅基地

文章浏览阅读2.1k次,点赞2次,收藏7次。Android实现(省、市、县)三级联动下拉框 下拉列表spinner主要实现办法:动态加载各级下拉值的适配器在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值_android省市县下拉框

amd为什么还用针脚_彻底翻盘!AMD 锐龙9 3900X与锐龙7 3700X评测-程序员宅基地

文章浏览阅读291次。前言AMD于一个月多前的台北电脑展上发布了新的Zen2架构锐龙3000系列处理器,代号“Matisse”。虽然之前大家已经收到许多消息,包括7nm工艺的进步,Zen2架构的chiplet多Die设计,IPC的提升等,但是对新处理器性能的表现还是有很大的期待。现在新的锐龙处理器3900X、3700X和X570主板已经来到PCEVA评测室,我们就一起来看看它的效能表现。Zen2架构的设计在实..._3900 pbo2

获得客户机IP,主机名,端口和用户,java获取客户机信息-程序员宅基地

文章浏览阅读1.6k次。request.getHeader("User-Agent"); //就是取得客户端的系统版本 request.getRemoteAddr(); //取得客户端的IP request.getRemoteHost() //取得客户端的主机名 request.getRemotePort(); //取得客户端的端口 request.getRemo..._如何获取发送请求的客户机的ip

QT+opencv安装笔记-程序员宅基地

文章浏览阅读175次。安装时出现的问题:我使用的build_opencv是别人编译好的,我不知道有可能会存在兼容性的问题(安装时报错,完成后缺失install文件)https://blog.csdn.net/qq_33308135/article/details/85049795复制的build_opencv文件位置:https://pan.baidu.com/s/1KsB553FYyLjmQ-j2OztxQQ...

NX二次开发基础_nx mcd二次开发-程序员宅基地

文章浏览阅读2.2k次。Nx的二次开发项目常用的创建方式有3种,1.NX开发向导(vc6中开发需要将UgOpen_18.awx和UgOpen_v18.hip文件复制到vs\common\msdev98\bin\ide目录)、2.WIN32应用程序向导、3.MFC应用程序向导。注册项目路径有2种方法:配置文件法和修改环境变量法1配置文件法:在配置文件custom_dirs.dat(位于%UGII_BASE_D..._nx mcd二次开发