Cod sursa(job #1021330)

Utilizator Athena99Anghel Anca Athena99 Data 3 noiembrie 2013 18:13:04
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int lmax= 26;
const int nmax= 1000000;

int u[lmax+1];
char v[nmax+1]; 

int main(  ) {
    fin>>v;
    int n= strlen(v);
    for ( int i= 0; i<n; ++i ) {
        ++u[v[i]-'a'];
    }

    int a= -1;
    for ( int i= 0; i<n; ++i ) {
        int p= -1;
        for ( int j= 0; j<lmax; ++j ) {
            if ( j!=a && u[j]==(n-i+3)/2 ) {
                p= j;
                j= lmax;
            }
        }
        if ( p>-1 ) {
            fout<<(char)(p+'a');
            --u[p];
            a= p;
        } else {
            for ( int j= 0; j<26; ++j ) {
                if ( a!=j && u[j]>0 ) {
                    fout<<(char)(j+'a');
                    --u[j];
                    a= j;
                    j= lmax;
                }
            }
        }
    }
    
    return 0;
}