146652025-01-26 17:01:15sarminVáltakozó (75 pont)cpp17Accepted 75/7575ms1212 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pp = pair<int, int>;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    
    string s; cin >> s;
    int n = s.length();
    vector<int> v(26);
    for (int i = 0; i < n; i++) {
      v[s[i] - 'a']++;
    }

    for (int i = 0; i < 26; i++) {
      if (v[i] > (n + 1) / 2) {
        cout << "-1";
        return 0;
      }
    }
    
    vector<char> res(n);
    int modifiedN = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 26; j++) {
        if (i > 0 && 'a' + j == res[i - 1]) continue;
        bool possible = true;
        for (int k = 0; k < 26; k++) {
          if ((k == j && v[k] - 1 > modifiedN / 2) || (k != j && v[k] > modifiedN / 2)) {
            possible = false;
          }
        }
        if (possible && v[j] > 0) {
          res[i] = 'a' + j;
          v[j]--;
          modifiedN--;
          break;
        }
      }
    }

    for (int i = 0; i < n; i++) cout << res[i];


    return 0;
}
SubtaskSumTestVerdictTimeMemory
base75/75
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted3/31ms316 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms508 KiB
7Accepted3/34ms496 KiB
8Accepted3/375ms1132 KiB
9Accepted3/32ms820 KiB
10Accepted3/32ms1144 KiB
11Accepted3/32ms548 KiB
12Accepted3/31ms316 KiB
13Accepted3/32ms316 KiB
14Accepted3/31ms508 KiB
15Accepted3/364ms1152 KiB
16Accepted3/359ms1144 KiB
17Accepted3/368ms1152 KiB
18Accepted3/371ms1208 KiB
19Accepted3/343ms1208 KiB
20Accepted3/339ms1212 KiB
21Accepted3/350ms1148 KiB
22Accepted6/637ms1212 KiB
23Accepted6/648ms1152 KiB
24Accepted6/675ms1148 KiB