/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 3ms 532.0 KiB
#3 Accepted 6ms 532.0 KiB
#4 Accepted 21ms 568.0 KiB
#5 Accepted 25ms 532.0 KiB
#6 Accepted 59ms 584.0 KiB
#7 Accepted 52ms 584.0 KiB
#8 Accepted 58ms 584.0 KiB
#9 Accepted 1ms 536.0 KiB
#10 Accepted 50ms 580.0 KiB
#11 Accepted 2ms 536.0 KiB
#12 Accepted 2ms 324.0 KiB
#13 Accepted 2ms 532.0 KiB
#14 Accepted 2ms 532.0 KiB
#15 Accepted 3ms 532.0 KiB

Code

#include <bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
// #define int long long
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define CheckBit(x,k)   (x & (1LL << k))
#define SetBit(x,k)     (x |= (1LL << k))
#define ClearBit(x,k)   (x &= ~(1LL << k))
#define toggleBit(x,k)  (x ^= (1LL << k))
#define LSB(mask)       __builtin_ctzll(mask)
#define MSB(mask)       63-__builtin_clzll(mask) 
#define print(x)    cout << #x << " : " << x << endl
#define error1(x)   cerr << #x << " = " << (x) <<endl
#define coutall(v)  for(auto &it: v) cout<<it<<' '; cout<<endl
#define Abid_52     ios::sync_with_stdio(false);cin.tie(0),cin.tie(0)
#define error2(a,b) cerr<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n"
#define UNIQUE(x)   sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T, typename U> T ceil(T x, U y) {return (x > 0 ? (x + y - 1) / y : x / y);}
template <typename T, typename U> T floor(T x, U y) {return (x > 0 ? x / y : (x - y + 1) / y);}

const int N = 2e5 + 10;

void solve()
{
    string s;
    cin >> s;
    int n = s.length();
    sort(all(s));
    // print(s);
    vector<int>cnt(28, 0);
    for(int i = 0; i < n; i++)
    {
        cnt[s[i] - 'a']++;
        if(cnt[s[i] - 'a'] > ceil(n, 2))
        {
            cout << -1 << endl;
            return;
        }
    }
    // coutall(cnt);
    int i = 0;
    string ans;
    while(i < 26)
    {
        if(cnt[i] > 0)
        {
            cnt[i]--;
            // cout << char('a' + i);
            ans.push_back(char('a' + i));
            for(int j = i + 1; j < 26; j++)
            {
                if(cnt[j] > 0)
                {
                    // cout << char('a' + j);
                    ans.push_back(char('a' + j));
                    cnt[j]--;
                    break;
                }
            }
        }
        else{
            bool ok = 0, ok1 = 0;
            for(int j = i + 1; j < 26; j++)
            {
                
                if(cnt[j] > 0 && !ok)
                {
                    ok = 1;
                    i = j;
                }
                if(cnt[j] > 0 && ans.back() != char('a' + j) && !ok1 && !ok)
                {
                    // cout << char('a' + j);
                    ans.push_back(char('a' + j));
                    cnt[j]--;
                    ok1 = 1;
                }
            }
            if(!ok)
            {
                break;
            }
        }
    }
    int x = n;
    // cout << ans << endl;
    // print(ans);
    while(x--)
    {
        for (int i = 0; i + 2 < n; i++)
        {
            if (ans[i] == ans[i + 1])
            {
                swap(ans[i + 1], ans[i + 2]);
            }
        }

        for (int i = n - 1; i  > 1; i--)
        {
            if (ans[i] == ans[i - 1])
            {
                swap(ans[i - 1], ans[i - 2]);
            }
        }
        // cout << ans << endl;
    }
    for (int i = 1; i + 1 < n; i++)
    {
        for (int j = i + 2; j + 1 < n; j++)
        {
            if ((ans[i] > ans[j]) && (ans[i - 1] != ans[j] && ans[i + 1] != ans[j]) && (ans[j - 1] != ans[i] && ans[j + 1] != ans[i]))
            {
                swap(ans[i], ans[j]);
            }
        }
    }
    // x = n;
    // while(x--)
    // {
    //     for (int i = n - 1; i  > 1; i--)
    //     {
    //         if (ans[i] == ans[i - 1])
    //         {
    //             swap(ans[i - 1], ans[i - 2]);
    //         }
    //     }
    // }
    // print(ans);

    // x = n;
    // while(x--)
    // {
    //     for(int i = 1; i + 1 < n; i++)
    //     {
    //         for(int j = i + 2; j + 1 < n; j++)
    //         {
    //             if((ans[i] > ans[j]) && (ans[i - 1] != ans[j] && ans[i + 1] != ans[j]) && (ans[j - 1] != ans[i] && ans[j + 1] != ans[i]))
    //             {
    //                 swap(ans[i], ans[j]);
    //             }
    //         }
    //     }
    // }


    // // print(ans);
    // for(int i = 1; i + 1 < n; i++)
    // {
    //     if ((ans[i - 1] != ans[i + 1]) && (ans[i + 2] != ans[i]) && (ans[i] > ans[i + 1]))
    //     {
    //         swap(ans[i], ans[i + 1]);
    //     }
    // }
    cout << ans << endl;
    
}

int32_t main()
{
    Abid_52;
    int t = 1;
    cin >> t;
    for (int tc = 1; tc <= t; ++tc)
    {
        // cout << "Case " << tc << ": ";
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1209 B. Rearrange the String
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-05 08:59:59
Judged At
2025-07-05 08:59:59
Judged By
Score
100
Total Time
59ms
Peak Memory
584.0 KiB