Cod sursa(job #2766693)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 2 august 2021 21:17:46
Problema Ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("ordine.in");
ofstream fout("ordine.out");

int main() {
  string s;
  fin >> s;
  int f[26] = {0};
  for (char c : s)
    ++f[c - 'a'];
  int n = s.size(), last = -1;
  while (n) {
    int best = -1;
    for (int c = 0; c < 26; ++c)
      if (c != last && f[c] > 0) {
        if (best == -1)
          best = c;
        else {
          if (f[c] > n / 2)
            best = c;
        }
      }
    fout << char('a' + best);
    --f[best];
    last = best;
    --n;
  }
  fout << '\n';
  fin.close();
  fout.close();
  return 0;
}