/ 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 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();
    int mxi = INT_MIN;
    map<char,int> seen;
    for (int i = 0; i < n; i++) {
        seen[s[i]] += 1;
        mxi = max(mxi, seen[s[i]]);
    }
    if (n - mxi < mxi - 1) {
        cout << -1 << '\n';
        return;
    }
    char prev = 'A';
    int total = 0;
    vector<char> ans;
    bool start = true;
    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;
        }
    }
    for (int i = 0; i < ans.size(); i++) {
        if (i != ans.size() - 1 && seen[ans[i + 1]] > 0) {
            cout << ans[i + 1];
            seen[ans[i + 1]] -= 1;
        }
        cout << ans[i];
    }
    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:17:57
Judged At
2025-07-14 16:17:57
Judged By
Score
0
Total Time
2ms
Peak Memory
532.0 KiB