/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 3ms 532.0 KiB
#4 Accepted 3ms 576.0 KiB
#5 Accepted 3ms 344.0 KiB
#6 Accepted 4ms 340.0 KiB
#7 Accepted 5ms 576.0 KiB
#8 Accepted 5ms 532.0 KiB
#9 Accepted 5ms 488.0 KiB
#10 Accepted 9ms 584.0 KiB
#11 Accepted 8ms 536.0 KiB
#12 Accepted 8ms 348.0 KiB
#13 Accepted 7ms 532.0 KiB
#14 Accepted 6ms 344.0 KiB
#15 Accepted 5ms 532.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define endl '\n'
#define ll long long
#define ld long double
#define vi vector<ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define srt(v) sort(v.begin(),v.end())
#define rep(i, a, b) for(ll i = (a); i < (b); i++)
#define trace(x) cout<<#x<<": "<<x<<endl;
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
const int N=5e5+10;
// void solve() {
//     string s;cin>>s;
//     ll n = sz(s);
//     map<char,ll>mpp;
//     vector<char>ans(n+1);
//     set<char>nts;
//     vector<bool> visited(n+1,false);
//     rep(i,0,n){
//         mpp[s[i]]++;
//         nts.insert(s[i]);
//     }
//     ll ini = 0;
//     ll cnt =0;
//     for(char c:nts){
//         for(int i = ini;i<n+1;i+=2){
//             if(mpp[c]==0){
//                 break;
//             }
//             if(!visited[i] && mpp[c]>0){
//                 ans[i] = c;
//                 cnt++;
//                 visited[i] = true;
//                 mpp[c]--;
//             }
//             else{
//                 i--;
//             }
            
//         }
//         //print(ans);
//         ini++;
//     }
//     for(int i =0;i<n;i++){
//         if(ans[i]==ans[i+1]){
//             cout<<-1<<endl;return;
//         }
//     }
//     for(int i = 0;i<n+1;i++){
//         cout<<ans[i];
//     }
//     cout<<endl;        
    

// }
bool is_ok(char c,map<char,ll>&mpp,char prev,ll baki){
    mpp[c]--;
    ll mx = 0;
    for(auto [x,y]:mpp){
        mx = max(mx,y);
    }
    mpp[c]++;
    return mx<=(baki)/2;
}
void solve(){
    string s;cin>>s;
    ll n = sz(s);
    map<char,ll>mpp;
    vector<bool>visited(n+1,false);
    rep(i,0,n){
        mpp[s[i]]++;
    }
    string ans ="";
    ll ini = -1;
    char prev ='0';
    rep(i,0,n){
        bool ok = false;
        for(char c='a';c<='z';c++){
            if(c == prev){
                continue;
            }
            if(mpp[c]==0){
                continue;
            }
            if(is_ok(c,mpp,prev,n-i)){
                ans +=c;
                mpp[c]--;
                prev  = c;
                ok  = true;
                break;
            }
        }
        if(!ok){
            cout<<-1<<endl;return;
        }
    }
    cout<<ans<<endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;cin>>t;
    for(int i =1;i<=t;i++){
        solve();
    }
    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:42:02
Judged At
2025-07-14 16:42:02
Judged By
Score
100
Total Time
9ms
Peak Memory
584.0 KiB