/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 436.0 KiB
#5 Accepted 1ms 532.0 KiB
#6 Accepted 1ms 572.0 KiB
#7 Accepted 1ms 580.0 KiB
#8 Accepted 1ms 532.0 KiB
#9 Accepted 1ms 532.0 KiB
#10 Accepted 2ms 528.0 KiB
#11 Accepted 1ms 492.0 KiB
#12 Accepted 1ms 532.0 KiB
#13 Accepted 2ms 444.0 KiB
#14 Accepted 1ms 352.0 KiB
#15 Accepted 1ms 532.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("O3")

typedef long long ll;
typedef unsigned int ull;
typedef long double lld;

#define int long long

/*---------------------------------------------------------------------------------------------------------------------------------------------*/
#ifdef ONLINE_JUDGE
#define debug(x)
#else
#define debug(x)       \
    cerr << #x << " "; \
    _print(x);         \
    cerr << endl;
#endif

#define int long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define setbits(x) __builtin_popcountll(x)

const int MOD = 1e9 + 7;

void _print(ll t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p)
{
    cerr << "{";
    _print(p.ff);
    cerr << ",";
    _print(p.ss);
    cerr << "}";
}
template <class T>
void _print(vector<T> v)
{
    cerr << "[ ";
    for (T i : v)
    {
        _print(i);
        cerr << " ";
    }
    cerr << "]";
}
template <class T>
void _print(set<T> v)
{
    cerr << "[ ";
    for (T i : v)
    {
        _print(i);
        cerr << " ";
    }
    cerr << "]";
}
template <class T>
void _print(multiset<T> v)
{
    cerr << "[ ";
    for (T i : v)
    {
        _print(i);
        cerr << " ";
    }
    cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v)
{
    cerr << "[ ";
    for (auto i : v)
    {
        _print(i);
        cerr << " ";
    }
    cerr << "]";
}

vector<int> vinput(int n)
{
    vector<int> v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
    }
    return v;
}

void printVector(vector<int> &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << '\n';
}

int ceilCustom(int a, int b)
{
    return (a + b - 1) / b;
}

/*--------------------------  CODE HERE -------------------------------------------------------------------------------------------------------------------*/

bool isPalindrome(string v, int l, int r)
{
    while (l < r)
    {
        if (v[l++] != v[r--])
            return false;
    }
    return true;
}

bool possible(vector<int>& cnt, int rem) {
    int mx = *max_element(cnt.begin(), cnt.end());
    return mx <= (rem + 1) / 2;
}

void solve()
{
    string s;
    cin >> s;

    int n = s.size();
    vector<int> mp(26, 0);
    for (int i = 0; i < n; i++)
    {
        mp[s[i] - 'a']++;
    }

    string ans;

    char prev = -1;
    for (int i = 0; i < n; i++) {
        bool placed = false;
        for (int j = 0; j < 26; j++) {
            if (mp[j] == 0) continue;
            if (i > 0 && ans[i-1] == char('a' + j)) continue;
            mp[j]--;
            if (possible(mp, n-i-1)) {
                ans.push_back('a'+j);
                placed = true;
                break;
            }
            mp[j]++;
        }
        if (!placed) {
            cout << -1 << '\n';
            return ;
        }
    }

    cout << ans << '\n';
}

signed main()
{

    fastio();

    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 17:08:45
Judged At
2025-07-14 17:08:45
Judged By
Score
100
Total Time
2ms
Peak Memory
580.0 KiB