Cod sursa(job #190185)

Utilizator alex23alexandru andronache alex23 Data 20 mai 2008 14:46:43
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <stdio.h>
#include <ctype.h>


struct aa{
int nr;
char ch;
};

aa sol[30];
int a[30],i,n,m,j;
char c,anterior;


int main()
 {FILE *f;

  f=fopen("ordine.in","r");
  fscanf(f,"%c",&c);
  m=0;
  while (!feof(f))
    {a[c-97]++;
     m++;
     fscanf(f,"%c",&c);
     }
  fclose(f);

  n=0;
  for (i=0;i<26;i++)
     if (a[i]!=0) {n++;sol[n].nr=a[i];sol[n].ch=i+97;}

  f=fopen("ordine.out","w");
  anterior=' ';
  for (i=1;i<=m;i++)
    if (sol[n].nr<=m-i) if (anterior!=sol[1].ch) {anterior=sol[1].ch;
                                                  fprintf(f,"%c",sol[1].ch);
                                                  sol[1].nr--;
                                                  if (sol[1].nr==0) {for (j=1;j<=n-1;j++)
                                                                       {sol[j].nr=sol[j+1].nr;
                                                                        sol[j].ch=sol[j+1].ch;
                                                                        }
                                                                     n--;
                                                                     }

                                                  }
                                            else {anterior=sol[2].ch;
                                                  fprintf(f,"%c",sol[2].ch);
                                                  sol[2].nr--;
                                                  if (sol[2].nr==0) {for (j=2;j<=n-1;j++)
                                                                       {sol[j].nr=sol[j+1].nr;
                                                                        sol[j].ch=sol[j+1].ch;
                                                                        }
                                                                     n--;
                                                                     }
                                                  }
                else {anterior=sol[n].ch;
                      fprintf(f,"%c",sol[n].ch);
                      sol[n].nr--;
                      if (sol[n].nr==0) n--;
                      }


  fclose(f);

  return 0;
  }