/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 348.0 KiB
#4 Accepted 1ms 324.0 KiB
#5 Accepted 1ms 532.0 KiB
#6 Accepted 2ms 504.0 KiB
#7 Accepted 1ms 388.0 KiB
#8 Accepted 1ms 564.0 KiB
#9 Accepted 1ms 532.0 KiB
#10 Accepted 2ms 524.0 KiB
#11 Accepted 1ms 324.0 KiB
#12 Accepted 1ms 484.0 KiB
#13 Accepted 2ms 532.0 KiB
#14 Accepted 1ms 532.0 KiB
#15 Accepted 1ms 564.0 KiB

Code

/**
*    In the name of Allah
*    We are nothing and you're everything
*    Ya Muhammad!
**/
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
using ull = uint64_t;
 
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
//#define int long long
 
const char nl = '\n';
const int N = 1e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3fll;

void solve() {
	string s; cin >> s;
	int n = sz(s);
	vector<int> a(30);
	for (auto i: s) {
		a[i-'a'] += 1;
	}
	
	string res;
	int m = n;
	//cout << a[1] << nl;
	for (int i = 0; i < n; ++i) {
		bool did = false;
		for (int j = 0; j < 27; ++j) {
			//if (i == 0 && j == 1)cout << (a[j]+1)/2 << " " <<  m-a[j] << nl;
			if (a[j]-1 == m-a[j] && (res.empty()||res.back()-'a' != j)) {
				res += char(j+(int)('a'));
				//cout << j << " " << i<< nl;
				a[j]--;
				m--;
				did = true;
				break;
			}
		}
		//cout << did << nl;
		if (!did) {
			for (int j = 0; j < 27; ++j)
				if ((res.empty()||res.back()-'a' != j) && a[j]>0) {
					did = true;
					res += char(j+(int)('a'));
					m--;a[j]--;
					break;
				}
		}
	}
	
	if (sz(res) < n)cout << -1 << nl;
	else cout << res << nl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t; 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:21:30
Judged At
2025-07-14 16:21:30
Judged By
Score
100
Total Time
2ms
Peak Memory
564.0 KiB