Cod sursa(job #187438)

Utilizator eugen.nodeaEugen Nodea eugen.nodea Data 4 mai 2008 10:18:36
Problema Ordine Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
# include <stdio.h>   
# include <string.h>

FILE *f=fopen("ordine.in","r");   
FILE *g=fopen("ordine.out","w");   
int Ap[133];
char uc,c,ch;
char s[1000001];
int k=0,x=0,ok=0;
long i;

int main()
{
  fscanf(f,"%s",s);
  k=strlen(s);
  for(i=0;i<strlen(s);i++)
   Ap[int(s[i])]=Ap[int(s[i])]+1;
  fclose(f);
  ch='a';
  while (!Ap[int(ch)])
   ch++;
  x=0;
  while (x<k){
   if (Ap[int(ch)])
   {
    x++;ok=0;
    for (c=ch;c<='z';c++)
      if (Ap[int(c)]>=(k-x+1)/2+1) {
		ok=1;
		break;
		  }
    if (ok) {
      fprintf(g,"%c",c);
      Ap[int(c)]--;
      uc=c;
    }
    else {
       if (uc!=ch) {
    fprintf(g,"%c",ch);
    Ap[int(ch)]--;
	uc=ch;
	}
	else {
	     ok=0;
	     for (c=ch+1;c<='z';c++)
	      if (Ap[int(c)]!=0) {
		    ok=1;
		    break;
		      }
	     if (ok)
	     {
	  fprintf(g,"%c",c);
	      uc=c;
	      Ap[int(c)]--;
	     }
	}
    }
    } else ch++;
    }
  fprintf(g,"\n");
  fclose(g);
  return 0;
}