Cod sursa(job #1605802)

Utilizator CraiuAndrei Craiu Craiu Data 19 februarie 2016 15:21:18
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

char s[1000005];
int f[30];
int n;

int main()
{
    int i, j;
    char x;
    fin >> s;
    n = strlen(s);
    for(i = 0; s[i]; i++)
    {
        j = s[i] - 'a';
        f[j]++;
    }
    i = 0;
    j = -1;
    while(n > 0)
    {
        while(f[i] == 0) i++;
        if(i != j)
            j = i + 1;
        else
        {
            j = i;
            i++;
            while(f[i] == 0) i++;
        }
        f[i]--;
        x = i + 'a';
        fout << x;
        n--;
        if(n > 0)
        {
            while(f[j] == 0) j++;
            f[j]--;
            x = j + 'a';
            fout << x;
            n--;
        }
    }
    return 0;
}