Cod sursa(job #127184)

Utilizator marinMari n marin Data 23 ianuarie 2008 16:09:46
Problema Restante Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <stdio.h>
#include <string.h>

#define DIM 36008


char w[DIM][20];
long int n,i,ii,jj,j;
char s[20];
int x,y;
int viz[DIM];


void creHeap(char v[DIM][20], long int n){
  long int i,c,p;
  char aux[20];
  for (i=2;i<=n;i++){
    c=i;
    p=i>>1;
    while ((p) && (strcmp(v[c],v[p])>0)) {
/*      aux = v[p];
      v[p] = v[c];
      v[c] = aux;*/
      strcpy(aux,v[p]);
      strcpy(v[p],v[c]);
      strcpy(v[c],aux);


      c = p;
      p = p>>1;
    }
  }
}

void heapSort(char v[DIM][20], long int n){
  long int i,p,c;
  char aux[20];
  creHeap(v,n);
  for (i=n;i>1;i--) {
    strcpy(aux,v[1]);
    strcpy(v[1],v[i]);
    strcpy(v[i],aux);
/*    aux = v[i];
    v[i]=v[1];
    v[1]=aux;*/

    p=1;
    c=p<<1;
    while ((c<=i-1) && (strcmp(v[c],v[p])>=0)) {
      if ((c+1<=i-1) && (strcmp(v[c+1],v[c])>0))
	c++;
/*      aux=v[p];
      v[p]=v[c];
      v[c]=aux;*/
      strcpy(aux,v[p]);
      strcpy(v[p],v[c]);
      strcpy(v[c],aux);
      p=c;
      c=p<<1;
    }

  }

}


int main(){
  FILE *f = fopen("restante.in","r");
  fscanf(f,"%ld",&n);
  for (i=1;i<=n;i++){
    fscanf(f,"%s",s);
    x = strlen(s);

    for (ii=0;ii<x-1;ii++)
      for (jj=ii+1;jj<=x-1;jj++)
	if (s[ii]>s[jj]) {
	  y = s[ii];
	  s[ii] = s[jj];
	  s[jj] = y;
	}


//    w[i]=new char[20];
    strcpy(w[i],s);
    int t=strlen(w[i]);
    while ((w[i][t]>'z')||(w[i][t]<'a')) {
      w[i][t]='\0';
      t--;
    }

  }
  fclose(f);

  heapSort(w,n);

  long int nr=0;
  for (i=2;i<n;i++)
    if ((strcmp(w[i],w[i-1])!=0)&&(strcmp(w[i],w[i+1])!=0)) {
      nr++;
    }
  if (strcmp(w[2],w[1])!=0)
    nr++;
  if (strcmp(w[n],w[n-1])!=0)
    nr++;


/*  j=1;
  for (i=2;i<=n;i++)
    if ((w[i]!=NULL) && (strcmp(w[i],w[j])!=0)) {
      j++;
      strcpy(w[j],w[i]);
    } else if (w[i]!=NULL) viz[j]=1;
*/


/*  for (i=1;i<=j;i++) {
    if (viz[i]==0)
      nr++;
  }*/

  FILE *g = fopen("restante.out","w");
  fprintf(g,"%ld",nr);
  fclose(g);

//  for (i=1;i<=n;i++)
//    printf("%s\n",w[i]);

//  for (i=1;i<=n;i++)
//    delete w[i];
//  printf("\n");
  return 0;
}