/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#if __has_include("../stdc++.h")
	#include "../stdc++.h"
#else
	#include <bits/stdc++.h>
#endif

template <typename T>
std::istream &operator>>(std::istream &in, std::vector<T> &v)
{
    for (T &x : v)
        in >> x;
    return in;
}

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v)
{
    for (std::size_t i = 0; i < v.size(); ++i)
        out << v[i] << (i + 1 == v.size() ? "" : " ");
    return out;
}

inline void yes() { std::cout << "Yes\n"; }
inline void no()  { std::cout << "No\n";  }

using namespace std;


/*

keep track of mxi

if there are more mxi than the rest

*/
void solve()
{
    int n;
    string s;
    cin >> s;
    n = s.size();
    map<char,int> seen;
    for (int i = 0; i < n; i++) {
        seen[s[i]] += 1;
    }
    char prev = 'A';
    int total = 0;
    vector<char> ans;
    for (int i = 97; i < 123; i++) {
        if (prev == i) continue;
        if (seen[i] > 0) {
            // cerr << "i: " << char(i) << '\n';
            // cerr << "seen[i]: " << seen[i] << '\n';
            ans.push_back(char(i));
            seen[i] -= 1;
            total += 1;
            prev = i;
            if (i != 97) i = 96;
        }
    }
    if (total != n) {
        int lookingFor = -1;
        for (auto v : seen) {
            if (v.second > 0) { 
                lookingFor = v.first;
                break;
            }
        }
        // cerr << "lookingFor: " << char(lookingFor) << '\n';
        vector<char> ans2;
        for (int i = ans.size() - 1; i >= 0; i--) {
            char curr = ans[i];
            // cerr << "Curr: " << curr << '\n';
            if (seen[lookingFor] <= 0) {
                // cerr << "juere" << '\n';
                ans2.push_back(ans[i]);
                continue;
            }
            if (i > 0 && curr != lookingFor && ans[i - 1] != lookingFor) {
                ans2.push_back(ans[i]);
                ans2.push_back(lookingFor);
                seen[lookingFor] -= 1;
            }
            else if (i == 0 && ans[i] != lookingFor) {
                ans2.push_back(ans[i]);
                ans2.push_back(lookingFor);
                seen[lookingFor] -= 1;
            } else {
                ans2.push_back(ans[i]);
            }
        }
        if (seen[lookingFor] <= 0) {
            for (int i = ans2.size() - 1; i >= 0; i--) {
                cout << ans2[i];
            }
            cout << '\n';
        }
        else {
            cout << -1 << '\n';
        }        
        return;
    }
    for (auto ss : ans) {
        cout << ss;
    }
    cout << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
        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:09:22
Judged At
2025-07-14 16:09:22
Judged By
Score
0
Total Time
2ms
Peak Memory
532.0 KiB