Codeforces-1251-C. Minimize The Integer(找规律+贪心)

C. Minimize The Integer

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

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): 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 <bits/stdc++.h>
//#include <iostream>
//#include <algorithm>
//#include <cstring>
//#include <cstdio>
//#include <map>
//#include <cstdlib>
#define read(x) scanf("%lld",&x)
#define re(n) for(int i = 0 ; i < n ; i ++)
#define rev(n) for(int i = n-1 ; i >= 0 ; i --)
#define fill(x,y) memset(x,y,sizeof(x))
const int N=1e6+10;
const int INF=0x3f3f3f3f;
typedef long long ll;
int a[N];
int mark[N];
char s1[N];
char s2[N];
using namespace std;

int main()
{
    ll n,m,i,j,k,t = 1;
    //freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    cin>>t;
    for(int cas = 1 ; cas <= t ; cas ++)
    {
        string s;
        cin>>s;
        int len = s.size();
        int pos1 = 0;
        int pos2 = 0;
        for(int i = 0 ; i < len ; i ++)
        {
            if((s[i] -'0') %2)
                s1[pos1++] = s[i];
            else
                s2[pos2++] = s[i];
        }
        int i = 0,j = 0;
        while(i < pos1 || j < pos2)
        {
            if(i >= pos1)
            {
                cout<<s2[j++];
            }
            else if(j >= pos2)
            {
                cout<<s1[i++];
            }
            else if(s1[i] <= s2[j])
            {
                cout<<s1[i++];
            }
            else
                cout<<s2[j++];
        }
        cout<<endl;
        //cout<<"Case "<<cas<<":"<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值