Pagini recente » Cod sursa (job #1952816) | Cod sursa (job #1274939) | Cod sursa (job #2940718) | Cod sursa (job #1636634) | Cod sursa (job #847738)
Cod sursa(job #847738)
#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';
}