点击打开链接
# include<stdio.h>
# include<iostream>
# include<string.h>
using namespace std;
struct shu
{
int num;
shu *next[30];
};
shu* creat()
{
shu *p;
int i;
p=new shu ;
p->num=0;
for(i=0;i<26;i++)
{
p->next[i]=NULL;
}
return p;
}
void insert(shu *p,char a[20])
{
int len,i,n;
shu *q;
len=strlen(a);
q=p;
for(i=0;i<len;i++)
{
n=a[i]-'a';
if(q->next[n]==NULL)
{
q->next[n]=creat();
q->next[n]->num++;
q=q->next[n];
}
else
{
q->next[n]->num++;
q=q->next[n];
}
}
}
int find(shu *p,char a[20])
{
shu *q,*t;
int len,i,n;
len=strlen(a);
q=p;
for(i=0;i<len;i++)
{
n=a[i]-'a';
if(q->next[n]==NULL)
{
return 0;
}
else
{
t=q->next[n];
q=q->next[n];
}
}
return t->num;
}
int main()
{
char a[20];
shu *p;
p=creat();
while(gets(a)&&a[0])
{
insert(p,a);
}
while(gets(a))
{
int count;
count=find(p,a);
printf("%d\n",count);
}
return 0;
}