Cod sursa(job #120972)

Utilizator eugen.nodeaEugen Nodea eugen.nodea Data 7 ianuarie 2008 15:05:44
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
# include <fstream.h>

fstream f("ordine.in",ios::in);
fstream g("ordine.out",ios::out);

int Ap[133];
unsigned char V[1000],uc,c,ch;

unsigned int k=0,x=0,ok=0;

int main()
{
  for (c='a';c<='z';c++)
	Ap[c]=0;
  f>>c;
  while (!f.eof()){
   Ap[c]=Ap[c]+1;
   k++;
   f>>c;
  }
  f.close();
  ch='a';
  while (!Ap[ch])
   ch++;
  x=0;
  while (x<k){
   if (Ap[ch])
   {
    x++;
    ok=0;
    for (c=ch;c<='z';c++)
      if (Ap[c]>=(k-x+1)/2+1) {
				ok=1;
				break;
			      }
    if (ok) {
      g<<c;
      Ap[c]--;
      uc=c;
    }
    else {
	   if (uc!=ch) {
		g<<ch;
		Ap[ch]--;
		uc=ch;
		}
	    else {
		     ok=0;
		     for (c=ch+1;c<='z';c++)
			  if (Ap[c]!=0) {
					ok=1;
					break;
				      }
		     if (ok)
		     {
		      g<<c;
		      uc=c;
		      Ap[c]--;
		     }
		}
	}
	} else ch++;
    }

  g.close();
  return 0;
}