Cod sursa(job #1835009)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 26 decembrie 2016 01:30:40
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;

int f[26];

int main(void) {
    ifstream fi("ordine.in");
    ofstream fo("ordine.out");
    string str, ans;
    int n, s;
    char ch, l;

    fi >> str, n = str.size();
    ans = str;

    s = n;
    for (auto i: str)
        ++f[i - 'a'];

    l = '\0';
    for (int i = 0; i < n; ++i, --s) {
        ch = 'z';
        for (int j = 0; j < 26; ++j) {
            if (f[j] >= n / 2 + 1) {
                ch = j + 'a';
                break; }
            if (f[j] && ('a' + j != l) && ('a' + j < ch))
                ch = 'a' + j; }
        l = ch;
        --f[ch - 'a'];
        ans[i] = ch; }

    fo << ans << '\n';

    return 0; }