Cod sursa(job #1835007)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 26 decembrie 2016 01:26:34
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 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) {
        for (int j = 0; j < 26; ++j) {
            if (f[j] >= n / 2) {
                ch = j + 'a';
                --f[j];
                break; }
            if (f[j] && 'a' + j != l) {
                ch = 'a' + j;
                --f[j];
                break; } }
        l = ch;
        ans[i] = ch; }

    fo << ans << '\n';

    return 0; }