#include<cstdio>
#include<cstdlib>
typedef struct node
{
int cnt;
struct node *next[26];
} AC;
AC * create()
{
AC *p=(AC *)malloc(sizeof(AC));
p->cnt=1;
for(int i=0;i<=25;i++)
p->next[i]=NULL;
return p;
}
void insert(AC *p,char *s)
{
int i=0;
while(s[i])
{
int k=s[i]-'a';
if(p->next[k])
p->next[k]->cnt++;
else
p->next[k]=create();
p=p->next[k];
i++;
}
}
int search(AC *p,char *s)
{
int i=0,ans=0;
while(s[i]!=0)
{
int k=s[i]-'a';
if(!p->next[k])return 0;
ans=p->next[k]->cnt;
p=p->next[k];
i++;
}
return ans;
}
int main()
{
AC *p=create();
char s[15];
while(gets(s)&&s[0])
{
insert(p,s);
}
while(scanf("%s",s)!=EOF)
{
printf("%d\n",search(p,s));
}
return 0;
}
hdu1251
字典樹入門~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.