/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 392.0 KiB
#2 Accepted 2ms 444.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 2ms 552.0 KiB
#5 Accepted 2ms 364.0 KiB
#6 Accepted 2ms 532.0 KiB
#7 Accepted 2ms 536.0 KiB
#8 Accepted 2ms 324.0 KiB
#9 Accepted 2ms 532.0 KiB
#10 Accepted 2ms 764.0 KiB
#11 Accepted 2ms 532.0 KiB
#12 Accepted 2ms 768.0 KiB
#13 Accepted 2ms 764.0 KiB
#14 Accepted 2ms 532.0 KiB
#15 Accepted 2ms 488.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
 
 
const int MOD = 1e9 + 7;
const int MX = 1e6 + 10;
long long dp[MX + 100][2];
 
 
void solve(){
  string s;
  cin >> s;
  int n = s.size();
  vector<int> freq(26);
  for(char i : s) freq[i-'a']++;
  string ans;
  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < 26 ; j++){
      if((ans.size() == 0 || ans.back() != char(j + 'a')) && freq[j]){
        int mx = 0;
        for(int k = 0 ; k < 26 ; k++){
          if(j != k) mx = max(mx,freq[k]);
        }
        if(2 * mx - (n - i - 1) <= 1){
          ans.push_back(char(j + 'a'));
          freq[j]--;
          break;
        }
      }
    }
  }
  if(ans.size() != n) cout << -1 << endl;
  else cout << ans << endl;
  
}
 
 
 
 
int main()
{
    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 16:01:56
Judged At
2025-07-14 16:01:56
Judged By
Score
100
Total Time
2ms
Peak Memory
768.0 KiB