/*
 * @(#) msu98_z.c - Problem 'Z' ("Unique word counter") solution
 * of the ACM Programming Contest at the MSU in 1998.
 * (c) 1998 Ivan Maidanski <ivmai@chat.ru> http://ivmai.chat.ru
 * Freeware program source. All rights reserved.
 **
 * Language: ANSI C
 * Tested with: Borland C++ v3.1
 * Last modified: 1998-04-04 16:35:00 GMT+04:00
 */

/* Input data file: z.dat */

#include <stdlib.h> /* NULL, malloc(), free() */
#include <stdio.h> /* FILE, fopen(), fscanf(), printf() */
#include <string.h> /* strcpy(), strcmp() */

char lowers[]="qwertyuiopasdfghjklzxcvbnm"; /* russian letters may be added */
char uppers[]="QWERTYUIOPASDFGHJKLZXCVBNM";

char word[256];

struct tree
{
 char *s;
 struct tree *plt,*pgt;
};

void to_upper(char *s)
{
 int i;
 for (;*s;s++)
  for (i=0;lowers[i];i++)
   if (*s==lowers[i])
   {
    *s=uppers[i];
    break;
   }
}

void add_word(struct tree **ppt)
{
 int r;
 if (*ppt!=NULL)
 {
  r=strcmp(word,(*ppt)->s);
  if (r<0)
   add_word(&((*ppt)->plt));
   else if (r>0)
    add_word(&((*ppt)->pgt));
 }
  else
  {
   *ppt=(struct tree *)malloc(sizeof(struct tree));
   (*ppt)->s=strcpy((char *)malloc(strlen(word)+1),word);
   (*ppt)->plt=(*ppt)->pgt=NULL;
  }
}

long del_tree(struct tree *pt)
{
 long cnt=0;
 if (pt!=NULL)
 {
  free((void *)pt->s);
  cnt=del_tree(pt->plt)+del_tree(pt->pgt)+1;
  free((void *)pt);
 }
 return cnt;
}

int main()
{
 int i,cnt;
 struct tree *pt;
 FILE *f=fopen("z.dat","rt");
 fscanf(f,"%*[^0-9]%d",&cnt);
 for (i=1;i<=cnt;i++)
 {
  for (pt=NULL;;)
  {
   if (fscanf(f,"%*[^A-Za-z]%[A-Za-z]", /* russian letters may be added */
       word)==EOF || !*word)
    break;
   if (!strcmp(word,"END"))
   {
    add_word(&pt);
    break;
   }
   to_upper(word);
   add_word(&pt);
  }
  printf("----- %d\n%ld\n",i,del_tree(pt));
 }
 return 0;
}
