Cod sursa(job #847738)

Utilizator stoicatheoFlirk Navok stoicatheo Data 4 ianuarie 2013 13:58:58
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
 
int main(){
    std::ifstream fin("ordine.in");
    std::ofstream fout("ordine.out");
    unsigned nrof[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    unsigned total=0;
    char c;
    fin.get(c);
    while(fin.good()&&c!='\n'){
        nrof[c-'a']++;
        ++total;
        fin.get(c);
    }
    unsigned idmax=100,max=0;
    for(unsigned i=0;i<26;++i)  
        if(nrof[i]>=max){
            max=nrof[i];
            idmax=i;
        }
    unsigned prev=100;
    while(total)
        if(max>total-max){
            fout<<static_cast<char>(idmax+'a'); prev=idmax;
            nrof[idmax]--; total--; max--;
            for(unsigned i=0;i<26;++i)
                if(nrof[i]>max){
                    max=nrof[i];
                    idmax=i;
                }
        }
        else{
            unsigned found;
            for(found=0;!nrof[found]||found==prev;found++);
            fout<<static_cast<char>(found+'a'); prev=found;
            nrof[found]--; --total;
            if(found==idmax){
                max--;
                for(unsigned i=0;i<26;++i)  
                    if(nrof[i]>max){
                        max=nrof[i];
                        idmax=i;
                    }
            }
        }
    fout<<'\n';
}