Cod sursa(job #1003813)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 1 octombrie 2013 17:42:41
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

const int NMAX = 1000010;

int N, Freq[30], Last = -1;
char S[NMAX];

int main()
{
    freopen("ordine.in", "r", stdin);
    freopen("ordine.out", "w", stdout);

    gets(S + 1);
    N = strlen(S + 1);

    for(int i = 1; i <= N; ++ i)
        Freq[ S[i] - 'a' ] ++;

    for(int i = 1; i <= N; ++ i)
    {
        bool Found = 0;
        for(int j = 0; j < 26 && !Found; ++ j)
            if(j != Last && Freq[j] == (N - i + 1) / 2 + 1)
            {
                Freq[j] --;
                Last = j;
                printf("%c", j + 'a');
                Found = 1;
            }
        if(!Found)
        {
            for(int j = 0; j < 26; ++ j)
                if(j != Last && Freq[j])
                {
                    printf("%c", j + 'a');
                    Freq[j] --;
                    Last = j;
                    break;
                }
        }
    }
}