Cod sursa(job #1274218)

Utilizator hopingsteamMatraguna Mihai-Alexandru hopingsteam Data 23 noiembrie 2014 16:39:21
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include    <fstream>
#include    <iostream>
#include    <algorithm>
#include    <cstring>

using namespace std;

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

char word[1000005];
int appears[30];
int L;

void Sort()
{
    int preLast = -1, LastC = -1;
    for(int i = 0; i < L; i++)
    {
        int aux = 0;
        while(!appears[aux] || aux == LastC)
            aux += 1;

        preLast = LastC; LastC = aux;

        for(aux = LastC + 1; aux < 26; aux += 1)
        {
            if(appears[aux] == (L - i) / 2 + 1 && aux != preLast)
                LastC = aux;
        }

        appears[LastC] -= 1;
        fout << (char)('a' + LastC);
    }
}

void read()
{
    fin >> word;
    L = strlen(word);
    for(int i = 0; i < L; i++)
    {
        int letter = (int)word[i] - 'a';
        appears[letter] += 1;
    }

    Sort();
}

int main()
{
    read();
    return 0;
}