Cod sursa(job #2024697)

Utilizator DruffbaumPopescu Vlad Druffbaum Data 21 septembrie 2017 00:10:22
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <cstdio>
#include <cstring>

const int MAXN = 1e6;
const int ALF = 26;

int fr[ALF];
char s[MAXN + 1], ans[MAXN + 1];

int main() {
  int n;
  bool check;
  FILE *f = fopen("ordine.in", "r");
  fscanf(f, "%s", s);
  fclose(f);
  n = strlen(s);
  for (int i = 0; i <= n; ++i) {
    ++fr[s[i] - 'a']; 
  }
  for (int i = 1; i <= n; ++i) {
    check = 0;
    for (int j = 0; j < ALF && !check; ++j) {
      if (fr[j] && ((n - i + 1) >> 1) + 1 <= fr[j] && ans[i - 1] != j + 'a') {
        ans[i] = j + 'a';
        --fr[j];
        check = 1;
      }
    }
    if (!check) {
      for (int j = 0; j < ALF && !check; ++j) {
        if (fr[j] && ans[i - 1] != j + 'a') {
          ans[i] = j + 'a';
          --fr[j];
          check = 1;
        }
      }
    }
  }
  ans[n + 1] = '\0';
  f = fopen("ordine.out", "w");
  fprintf(f, "%s\n", ans + 1);
  fclose(f);
  return 0;
}