Cod sursa(job #2155262)

Utilizator Andrei17Andrei Pascu Andrei17 Data 7 martie 2018 18:58:22
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <string.h>

using namespace std;

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

const int N = 1000001;

int n, c[26];
char s[N], v[N];

int main()
{
    int vf = 0;
    in >> s;
    n = strlen(s);

    for (int i = 0; i < n; i++) c[ s[i] - 'a' ]++;
    in.close();

    for (int i = 0; i < n; i++) {
        int poz = -1;

        for (int j = 0; j < 26; j++) {
            if (v[vf] != (char)(j + 'a') && c[j] - 1 >= n - vf - c[j]) poz = j;
        }

        if (poz == -1) {
            for (poz = 0; poz < 26; poz++) {
                if (c[poz] != 0 && v[vf] != (char)(poz + 'a')) break;
            }
        }
        v[++vf] = (char)(poz + 'a');
        c[poz]--;
    }

    out << 1 + v;
    out.close();
}