Pagini recente » Calibrare limite de timp | Calibrare limite de timp | Istoria paginii runda/concurs_pd/clasament | Istoria paginii runda/simulare.oji.2015/clasament | Cod sursa (job #340873)
Cod sursa(job #340873)
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#define MAXC 27
using namespace std;
long xx,v[MAXC],ok,ok2,ok3,q,qq,i,st1,st2,k;
string s;
int main(){
ifstream fin("ordine.in");
ofstream fout("ordine.out");
fin >> s;
for (i=0; i<(s.length()); i++){
v[int(s[i])-96]++;
}
st1=1;
st2=2;
q=0;
qq=0;
ok2=0;
long a=s.length();
while (st1<27 || st2<27){
q++;
qq++;
ok=0;
ok3=0;
if(qq%2==1){
while((v[st1]==0 && st1<27) || (st1==st2 && st1<27)) ++st1;
if(st1<27){
if (ok2==0) for (i=1; i<=26; i++){
if(v[i]>=(a-q+1)/2+1){
if(st2<st1){ xx=st2; st2=i; st1=xx;}
else {st2=i;}
//ok2=1;
ok3=1;
break;
}
}
if(ok3==0){
s[q-1]=char(st1+96);
v[st1]--;
ok=1;
}
}
}
if(ok==0){
while((v[st2]==0 && st2<27) || (st1==st2 && st2<27)) ++st2;
if(st2<27){
if (ok2==0) for (i=1; i<=26; i++){
if(v[i]>=(a-q+1)/2+1){
if(st1<st2){ xx=st1; st1=i; st2=xx;}
else {st1=i;}
//ok2=1;
ok3=1;
break;
}
}
if(ok3==0){
s[q-1]=char(st2+96);
v[st2]--;
ok=1;
}
}
}
if(ok==0) q--;
}
fout << s;
return 0;
}