Cod sursa(job #1743369)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 august 2016 01:53:34
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <fstream>
#include <cstring>
using namespace std;

ifstream cin("ordine.in");
ofstream cout("ordine.out");

const int MAXN = 1000000;
const int SIGMA = 26;

char s[1 + MAXN];
int frequency[SIGMA];

int main() {
    cin >> s;
    int n = strlen(s), previous = -1;
    for (int i = 0; i < n; i++)
        frequency[s[i] - 'a']++;
    for (int i = 1; i <= n; i++) {
        bool found = false;
        for (int j = 0; j < SIGMA && !found; j++)
            if (frequency[j] && frequency[j] >= (n - i + 1) / 2 + 1 && j != previous) {
                cout << (char) (j + 'a');
                previous = j;
                frequency[j]--;
                found = true;
            }
        if (!found)
            for (int j = 0; j < SIGMA && !found; j++)
                if (frequency[j] && j != previous) {
                    cout << (char) (j + 'a');
                    previous = j;
                    frequency[j]--;
                    found = true;
                }
    }
    return 0;
}