/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 3ms 532.0 KiB
#5 Accepted 4ms 324.0 KiB
#6 Accepted 8ms 572.0 KiB
#7 Accepted 5ms 532.0 KiB
#8 Accepted 5ms 532.0 KiB
#9 Accepted 5ms 532.0 KiB
#10 Accepted 9ms 532.0 KiB
#11 Accepted 7ms 532.0 KiB
#12 Accepted 3ms 532.0 KiB
#13 Accepted 3ms 532.0 KiB
#14 Accepted 2ms 324.0 KiB
#15 Accepted 2ms 536.0 KiB

Code

/*
 *Copyright (c) Swadheen Islam Robi (SIR01)
 *Created on Mon Jul 14 2025 8:45:49 PM
 */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl "\n"
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define mod 1000000007
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define ALLAHU_AKBAR ios::sync_with_stdio(false); cin.tie(nullptr);

ll mx(map<char,ll>&mp)
{
    ll m = 0; ll x;
    for(auto &[a,b]:mp)
    {
        if(b>m) m = b;
    }
    return m;
}

void sir()
{
    // Bismillahir Rahmanir Rahim 

    string s; cin>>s;
    ll n = s.length();

    map<char,ll>mp;

    for(auto &c:s)mp[c]++;
    
    ll hi = mx(mp);
    ll rem = n-hi;
    if(hi-rem>1)
    {
        cout<<-1<<endl;
        return;
    }
    ll use = n;
    string ans = "";

    for(ll i=1; i<=n; i++)
    {
        ll c_mx = mx(mp);
        ll baki = use - c_mx;
        if(c_mx>baki)
        {
            for(char c = 'a'; c<='z'; c++)
            {
                if(mp[c]==c_mx) 
                {
                    ans += c;
                    mp[c]--;
                    break;
                }
            }
        }
        else{
            for(char c='a'; c<='z'; c++)
            {
                if(mp[c] && ans[ans.length()-1] !=c)
                {
                    ans += c;
                    mp[c]--;
                    break;
                }
            }
        }
        use--;
    }
    cout<<ans<<endl;


}

int main() {
    ALLAHU_AKBAR

    ll t = 1;
    cin>>t;
    while(t--)
    {
        sir();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1209 B. Rearrange the String
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 19:50:19
Judged At
2025-07-14 19:50:19
Judged By
Score
100
Total Time
9ms
Peak Memory
572.0 KiB