/*
昨天在小策策 和 小超人的指导下学习了下字典树,于是今天写了下这个很早就想学的算法
代码写的很丑,而且还看了下课件的
*/
#include<iostream>//2497868 2010-05-28 21:25:42 Accepted 1251 93MS 43784K 1152 B C++ 悔惜晟
#include<cstring>
#include<cstdio>
using namespace std;
struct trie
{
struct trie *child[26];
int n;
};
struct trie *root;
void insert(char *str)
{
int i, j, len;
len = strlen(str);
if(len == 0)
return ;
struct trie *P, *N;
P = root;
for(i = 0 ; i < len; i++)
{
if(P->child[str[i] - 'a'] != NULL)
{
P = P->child[str[i] - 'a'];
P->n = P->n + 1;
}
else
{
N = (struct trie *)malloc(sizeof(struct trie ));
for(j = 0; j < 26; j++)
N->child[j] = NULL;
P->child[str[i] - 'a'] = N;
P = N;
P->n = 1;
}
}
}
int find(char *str)
{
int i, len, m;
len = strlen(str);
struct trie *P;
P = root;
for(i = 0; i < len; i++)
{
if(P->child[str[i] - 'a'] != 0)
{
P = P->child[str[i] - 'a'];
m = P->n;
}
else
return 0;
}
return m;
}
int main()
{
char str[20];
root = (struct trie *)malloc(sizeof(struct trie));
int i;
for(i = 0; i < 26; i++)
root->child[i] = NULL;
while(gets(str))
{
if(strcmp(str, "") == 0)
break;
insert(str);
}
while(gets(str) )
{
printf("%d/n", find(str));
}
}