/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 2ms 532.0 KiB
#3 Accepted 3ms 484.0 KiB
#4 Accepted 3ms 444.0 KiB
#5 Accepted 3ms 356.0 KiB
#6 Accepted 3ms 592.0 KiB
#7 Accepted 2ms 532.0 KiB
#8 Accepted 2ms 532.0 KiB
#9 Accepted 2ms 320.0 KiB
#10 Accepted 6ms 576.0 KiB
#11 Accepted 6ms 532.0 KiB
#12 Accepted 6ms 532.0 KiB
#13 Accepted 8ms 532.0 KiB
#14 Accepted 5ms 320.0 KiB
#15 Accepted 4ms 320.0 KiB

Code

/**
 *    author:  RedImposter
 *    created: 17.06.2025 17:11:14
 **/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define life ios_base::sync_with_stdio(false);
#define saver cin.tie(0);
#define f first
#define sc second
#define rep(i, a, n) for (long long int i = (a); i <= (n); ++i)
#define repI(i, a, n) for (int i = (a); i <= (n); ++i)
#define repD(i, a, n) for (long long int i = (a); i >= (n); --i)
#define repDI(i, a, n) for (int i = (a); i >= (n); --i)
#define nxt cout << endl;
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define rall(x) (x).rbegin(), (x).rend()
#define INF (ll)1e18
#define inp(a)                        \
   for (int i = 0; i < a.size(); i++) \
   cin >> a[i]
#define out(a)                        \
   for (int i = 0; i < a.size(); i++) \
   cout << a[i] << " "
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<long long int> vll;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

void RedImposter()
{
   string s, t;
   cin >> s;

   sort(all(s));
   int n = s.size();

   map<char, int> mp;
   int maxi = 0;

   for (int i = 0; i < n; i++)
   {
      mp[s[i]]++;
      maxi = max(maxi, mp[s[i]]);
   }

   if (maxi > (n + 1) / 2)
   {
      cout << -1;
      nxt return;
   }
   int j = 0;

   while (t.size() != n)
   {

      int m = n - t.size();
      int maxi2 = 0;
      char c;
      for (auto it : mp)
      {
         if (maxi2 < mp[it.f])
         {
            maxi2 = mp[it.f];
            c = it.f;
         }
      }
      // debug(maxi2,(m+1)/2);
      if ((m & 1) && maxi2 == (m + 1) / 2)
      {
         t += c;
         mp[c]--;
         continue;
      }
      // debug(t,s);
      for (auto it : mp)
      {
         if (it.f != t.back() && it.sc > 0)
         {
            t += it.f;
            mp[it.f]--;
            break;
         }
      }
   }

   cout << t;
   nxt
}

int main()
{
   life saver int t;
   cin >> t;
   while (t--)
      RedImposter();
   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:49:38
Judged At
2025-07-14 16:49:38
Judged By
Score
100
Total Time
8ms
Peak Memory
592.0 KiB