/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tc; cin >> tc;

    test:
    while (tc--) {
        string s; cin >> s;
        ll n = s.size();

        ll cnt[26] = {0};
        for (auto &u : s) cnt[u-'a']++;

        string ans;
        for (ll i = 0; i < 26; i++) {
            if (ans.size() and ans.back()-'a' == i) continue;
            while (cnt[i] > 0) {
                ll jj = i;
                for (ll j = 0; j < 26; j++) {
                    if (j != i and cnt[j]) {
                        jj = j;
                        break;
                    }
                }
                if (jj == i) {
                    ans += char('a'+i);
                    cnt[i]--;
                    break;
                }

                ans += char('a'+i);
                ans += char('a'+jj);
                cnt[i]--;
                cnt[jj]--;
            }
        }

        char ch = 'A';
        for (ll i = 0; i < 26; i++) {
            if (cnt[i] == 0) continue;
            if (cnt[i] > 1) {
                cout << "-1\n";
                goto test;
            }
            ch = char(i+'a');
        }

        
        vector<string> v;
        if (ch != 'A') {
            ll m = ans.size();
            for (ll i = 1; i < ans.size(); i++) {
                if (ans[i] == ch) {
                    string temp;
                    for (ll j = 0; j < i-1; j++) temp += ans[j];
                    temp += ch;
                    for (ll j = i-1; j < m; j++) temp += ans[j];
                    v.push_back(temp);
                    break;
                }
            }
            v.push_back(ans+ch);
            v.push_back(ch+ans);
        }
        else v.push_back(ans);
        sort(v.begin(), v.end());

        for (auto &u : v) {
            ll check = true;
            for (ll i = 1; i < n; i++) {
                if (u[i] == u[i-1]) {
                    check = false;
                    break;
                }
            }

            if (check) {
                cout << u << "\n";
                goto test;
            }
        }


        cout << -1 << "\n";
    }
}

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