CodeForces - 1251C (thinking + + greedy merge sort)

The meaning of problems

https://vjudge.net/problem/CodeForces-1251C

A string of adjacent odd-even number can not exchange position, other neighboring circumstances can exchange, ask string representing the minimum number is.

Thinking

Adjacent even, odd fixed position, it is possible to put together the odd, even-put together, these piles can merge sort.

Code

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qpow(ll a,ll b){ll res=1;while(b){if(b&1) res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
ll inv(ll a,ll p){return qpow(a,p-2);}
int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        string ji,ou;
        int l=s.length();
        for(int i=0;i<l;i++)
        {
            if((s[i]-'0')%2)
                ji+=s[i];
            else
                ou+=s[i];
        }
        int i=0,j=0,li=ji.length(),lj=ou.length();
        while(i<li&&j<lj)
        {
            if(ji[i]<ou[j])
            {
                cout<<ji[i];
                i++;
            }
            else
            {
                cout<<ou[j];
                j++;
            }
        }
        while(i<li)
            cout<<ji[i++];
        while(j<lj)
            cout << or [j ++]; 
        cout << endl; 
    } 
    Return 0; 
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11846308.html