Wrong Answer
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int tc; cin >> tc;
test:
while (tc--) {
string s; cin >> s;
ll n = s.size();
ll cnt[26] = {0};
for (auto &u : s) cnt[u-'a']++;
string ans;
ll remaining = n;
while (remaining > 0) {
ll curr = -1;
for (ll i = 0; i < 26; i++) {
if (ans.size() and ans.back()-'a' == i) continue;
if (curr == -1 and cnt[i]) curr = i;
if (remaining-cnt[i] < cnt[i]) {
curr = i;
break;
}
}
ll j = -1;
for (ll i = 0; i < 26; i++) {
if (i == curr) continue;
if (cnt[i]) {
j = i;
break;
}
}
if (j == -1) {
ans += char('a'+curr);
cnt[curr]--;
remaining--;
break;
}
else {
ans += char('a'+curr);
ans += char('a'+j);
cnt[curr]--;
cnt[j]--;
remaining -= 2;
}
// cout << ans << endl;
}
if (remaining) {
cout << "-1\n";
goto test;
}
for (ll i = 1; i < n; i++) {
if (ans[i] == ans[i-1]) {
cout << "-1\n";
goto test;
}
}
cout << ans << "\n";
}
}
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:04:13
- Judged At
- 2025-07-14 17:04:13
- Judged By
- Score
- 0
- Total Time
- 1ms
- Peak Memory
- 532.0 KiB