/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 2ms 532.0 KiB
#3 Wrong Answer 4ms 532.0 KiB

Code

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// #define int long long
const int MOD = 1000000007;
#define sz(x) (ll)(x).size()
#define rd ({ll x; cin >> x; x; })
#define dbg(x) cerr << "[" #x "]  " << (x) << "\n"
// #define errv(x) {cerr << "["#x"]  ["; for (const auto& ___ : (x)) cerr << ___ << ", "; cerr << "]\n";}
// #define cerr if(0)cerr
#define xx first
#define yy second
mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
/*_________________________________________________________________________________________________________________________*/

void Solve()
{
    string s;
    cin >> s;
    ll n = sz(s);
    map<char, ll> cnt;
    for (auto c : s) {
        cnt[c]++;
    }
    for (auto& [c, v] : cnt) {
        if (v > (n + 1) / 2) {
            cout << "-1\n";
            return;
        }
    }
    sort(s.begin(), s.end());
    // for (ll bar = n; bar >= 0; --bar) {
    //     for (ll i = 1; i < n - 1; i++) {
    //         if (s[i] == s[i - 1])
    //             swap(s[i], s[i + 1]);
    //     }
    // }
    // for (ll bar = n; bar >= 0; --bar) {
    //     for (ll i = 1; i < n - 1; i++) {
    //         if (s[i] == s[i - 1])
    //             swap(s[i], s[i + 1]);
    //     }
    // }
    // for (ll bar = n; bar >= 0; --bar) {
    //     for (ll i = n - 2; i >= 1; i--) {
    //         if (s[i] == s[i + 1])
    //             swap(s[i], s[i - 1]);
    //     }
    // }
    // for (ll bar = n; bar >= 0; --bar) {
    //     for (ll i = n - 2; i >= 1; i--) {
    //         if (s[i] == s[i + 1])
    //             swap(s[i], s[i - 1]);
    //     }
    // }
    // cout << s << "\n";
    deque<pair<char, ll>> l, r;
    for (auto& it : s)
        if (r.empty() || r.back().first != it) {
            r.push_back({ it, 1 });
        } else {
            r.back().second++;
        }
    for (ll bar = n; bar >= 0; --bar) {
        while (!r.empty()) {
            if (sz(r) == 1) {
                l.push_back(r.front());
                r.pop_front();
                break;
            }
            auto f1 = r.front();
            r.pop_front();
            auto f2 = r.front();
            r.pop_front();
            if (f1.xx == f2.xx) {
                r.push_front({ f1.xx, f1.yy + f2.yy });
            } else if (f1.yy >= 2) {
                f1.yy--;
                f2.yy--;
                l.push_back({ f1.xx, 1 });
                l.push_back({ f2.xx, 1 });
                if (f2.yy)
                    r.push_front(f2);
                r.push_front(f1);
            } else {
                l.push_back(f1);
                r.push_front(f2);
            }
        }
        r = l;
        l = deque<pair<char, ll>>();
    }
    // for (auto& it : r)
    //     cout << it.xx;
    // cout << '\n';
    for (auto& it : r)
        l.push_front(it);
    r = l;
    l = deque<pair<char, ll>>();
    // for (auto& it : r)
    //     cout << it.xx;
    // cout << '\n';
    for (ll bar = n; bar >= 0; --bar) {
        while (!r.empty()) {
            if (sz(r) == 1) {
                l.push_back(r.front());
                r.pop_front();
                break;
            }
            auto f1 = r.front();
            r.pop_front();
            auto f2 = r.front();
            r.pop_front();
            if (f1.xx == f2.xx) {
                r.push_front({ f1.xx, f1.yy + f2.yy });
            } else if (f1.yy >= 2) {
                f1.yy--;
                f2.yy--;
                l.push_back({ f1.xx, 1 });
                l.push_back({ f2.xx, 1 });
                if (f2.yy)
                    r.push_front(f2);
                r.push_front(f1);
            } else {
                l.push_back(f1);
                r.push_front(f2);
            }
        }
        r = l;
        l = deque<pair<char, ll>>();
    }
    for (auto& it : r)
        l.push_front(it);
    r = l;
    for (auto& it : r)
        cout << it.xx;
    cout << '\n';
}

int32_t main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        // cout << "Case #" << i << ": "; // cout << "Case " << i << ": ";
        Solve();
    }
    return 0;
}
// Coded by Tahsin Arafat (@TahsinArafat)

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 17:33:03
Judged At
2025-07-14 17:33:03
Judged By
Score
0
Total Time
4ms
Peak Memory
532.0 KiB