/**
[字典树 ]hdu 1251 统计难题
统计前缀出现次数
*/
#include <stdio.h>
#include <string.h>
#define N 1000000
struct tireTree
{
int cnt;
tireTree *child[26];
}tt[N],*spt;
void insert(char *s,tireTree *&rt)
{
tireTree *loc;
if(rt == NULL)
rt = spt++;
loc = rt;
int num;
for(int i = 0; s[i]; ++i)
{
num = s[i] - 'a';
if(loc->child[num] == NULL)
loc->child[num] = spt++;
loc = loc->child[num];
loc->cnt++;
}
}
int query(char *s,tireTree *rt)
{
tireTree *loc = rt;
if(rt == NULL)
return 0;
int num;
for(int i = 0; s[i]; ++i)
{
num = s[i] - 'a';
if(loc->child[num] == NULL)
return 0;
loc = loc->child[num];
}
return loc->cnt;
}
int main()
{
char s[11];
tireTree *rt;
rt = NULL;
spt = tt;
while(gets(s))
{
if(s[0] == 0)
break;
insert(s,rt);
}
while(scanf("%s",s) == 1)
printf("%d\n",query(s,rt));
return 0;
}
[字典树 ]hdu 1251 统计难题
最新推荐文章于 2018-08-02 16:48:38 发布