#line 1 "wf1.c"


struct node {
	int count;
	struct node *left;
	struct node *right;
	char *word;
} words[2000];
int next;

struct node *lookup();

main() {
	struct node *root;
	char word[20];

	root = 0;
	next = 0;
	while (getword(word))
		lookup(word, &root)->count++;
	tprint(root);
	return 0;
}


err(s) char *s; {
	printf("? %s\n", s);
	exit(1);
}


int getword(buf) char *buf; {
	char *s;
	int c;

	while ((c = getchar()) != -1 && isletter(c) == 0)
		;
	for (s = buf; c = isletter(c); c = getchar())
		*s++ = c;
	*s = 0;
	if (s > buf)
		return (1);
	return (0);
}


int isletter(c) {
	if (c >= 'A' && c <= 'Z')
		c += 'a' - 'A';
	if (c >= 'a' && c <= 'z')
		return (c);
	return (0);
}


struct node *lookup(word, p) char *word; struct node **p; {
	int cond;
	char *malloc();

	if (*p) {
		cond = strcmp(word, (*p)->word);
		if (cond < 0)
			return lookup(word, &(*p)->left);
		else if (cond > 0)
			return lookup(word, &(*p)->right);
		else
			return *p;
	}
	if (next >= 2000)
		err("out of node storage");
	words[next].count = 0;
	words[next].left = words[next].right = 0;
	words[next].word = malloc(strlen(word) + 1);
	if (words[next].word == 0)
		err("out of word storage");
	strcpy(words[next].word, word);
	return *p = &words[next++];
}


tprint(tree) struct node *tree; {
	if (tree) {
		tprint(tree->left);
		printf("%d\t%s\n", tree->count, tree->word);
		tprint(tree->right);
	}
}

/* strcmp - compare s1 and s2, return <0, 0, or >0 */
static int strcmp(s1, s2) char *s1, *s2; {
	while (*s1 == *s2) {
		if (*s1++ == 0)
			return 0;
		++s2;
	}
	if (*s1 == 0)
		return -1;
	else if (*s2 == 0)
		return 1;
	return *s1 - *s2;
}
