/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 2ms 532.0 KiB
#3 Accepted 2ms 508.0 KiB
#4 Accepted 2ms 532.0 KiB
#5 Accepted 2ms 532.0 KiB
#6 Accepted 2ms 576.0 KiB
#7 Accepted 2ms 484.0 KiB
#8 Accepted 1ms 532.0 KiB
#9 Accepted 1ms 576.0 KiB
#10 Accepted 2ms 532.0 KiB
#11 Accepted 2ms 488.0 KiB
#12 Accepted 2ms 508.0 KiB
#13 Accepted 2ms 540.0 KiB
#14 Accepted 2ms 532.0 KiB
#15 Accepted 2ms 532.0 KiB

Code

// Created: 2025-07-14 21:00:35 IST
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define int long long
#define ll long long
#define cout cout<<fixed<<setprecision(0)
#define pb push_back
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define f(a,n) for(int i=0; i<n; i++){ cin>>a[i]; }
#define ff(a,l,n) for(int i=l; i<=n; i++){ cin>>a[i]; }
#define vvi vector<vector<int>>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define minpq priority_queue<ll, vector<ll>, greater<ll>> ;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void solve(){
    string s; cin>>s;
    int n=s.length();   
    map<int,int> mp;
    for(int i=0; i<n; ++i){
        ++mp[s[i]-'a'];
    }
    string ans;
    for(int i=0; i<n; ++i){
        int c=-1;
        for(auto [key,val]:mp){
            if(val>(n-i)/2){
                c=key;
                break;
            }
        }
        if(c>=0){
            ans.pb(c+'a');
            --mp[c];
            if(mp[c]==0){
                mp.erase(c);
            }
        }
        else{
            auto it=mp.begin();
            if(i>0 &&it->first==ans[i-1]-'a'){
                ++it;
                if(it==mp.end()){
                    cout<<-1<<endl;
                    return;
                }
            }
            c=it->first;
            ans.pb(c+'a');
            --mp[c];
            if(mp[c]==0){
                mp.erase(c);
            }
        }
    }
    for(int i=0; i<n-1; ++i){
        if(ans[i]==ans[i+1]){
            cout<<-1<<endl;
            return;
        }
    }

    cout<<ans<<endl;
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    // solve();
    int t; cin>>t;
    for(int i=0; i<t; i++){
        solve();
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    // cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0; 
}

Information

Submit By
Type
Submission
Problem
P1209 B. Rearrange the String
Contest
Educational Round 1
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 16:52:02
Judged At
2025-07-14 16:52:02
Judged By
Score
100
Total Time
2ms
Peak Memory
576.0 KiB