115282024-10-19 10:21:24MagyarKendeSZLGVáltakozó (75 pont)cpp17Accepted 75/7543ms1308 KiB
#include <bits/stdc++.h>
using namespace std;

bool can_finish(const array<int, 26>& h, int j) {
    int sum = 0, mx = 0, mxi = -1;
    for (int i = 0; i < 26; i++) {
        sum += h[i];
        if (mx < h[i]) {
            mx = h[i];
            mxi = i;
        }
    }
    return mx <= sum / 2 + (sum % 2 != 0 && j != mxi);
}

int main() {
    string s;
    cin >> s;

    array<int, 26> h = {0};
    for (char c : s) h[c - 'a']++;

    string result;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < 26; j++) {
            h[j]--;
            if (h[j] >= 0 && can_finish(h, j) &&
                (result.empty() || result.back() != j + 'a')) {
                result += j + 'a';
                break;
            }
            h[j]++;
        }
    }

    cout << (result.size() == s.size() ? result : "-1");
}
SubtaskSumTestVerdictTimeMemory
base75/75
1Accepted0/01ms320 KiB
2Accepted0/01ms320 KiB
3Accepted3/31ms500 KiB
4Accepted3/31ms320 KiB
5Accepted3/31ms400 KiB
6Accepted3/31ms320 KiB
7Accepted3/33ms320 KiB
8Accepted3/319ms1132 KiB
9Accepted3/325ms712 KiB
10Accepted3/325ms728 KiB
11Accepted3/31ms320 KiB
12Accepted3/31ms320 KiB
13Accepted3/31ms320 KiB
14Accepted3/31ms320 KiB
15Accepted3/318ms1144 KiB
16Accepted3/318ms1308 KiB
17Accepted3/325ms1184 KiB
18Accepted3/332ms1112 KiB
19Accepted3/318ms1240 KiB
20Accepted3/317ms1112 KiB
21Accepted3/321ms1144 KiB
22Accepted6/617ms1252 KiB
23Accepted6/619ms1112 KiB
24Accepted6/643ms1140 KiB