统计难题
注意:本题只有一组测试数据,处理到文件结束.
banana band bee absolute acm ba b band abc
2 3 1 0
#include <cstdio>
#include <cstring>
char s[11];
int ans;
struct node
{
int cnt;
node *next[28];
node()
{
memset(next,NULL,sizeof(next));
cnt=0;
}
};
void Inseart(node *p,char *s)
{
int b;
for(int i=0;s[i]!='\0';i++)
{
b=s[i]-'a';
if(p->next[b]==NULL)
p->next[b]=new node();
p=p->next[b];
p->cnt++; //记录前缀个数,注意写在p=p->next[b]后面,否则最后一字母会忽略。
}
return ;
}
void Search(node *p,char *s)
{
for(int i=0;s[i]!='\0';i++)
{
int a=s[i]-'a';
if(p->next[a]==NULL)
return ;
p=p->next[a];
}
ans=p->cnt;
}
int main()
{
node *root=new node();
while(true)
{
gets(s);
if(!strlen(s))break;
Inseart(root,s);
}
while(scanf("%s",s)!=EOF)
{
ans=0;
Search(root,s);
printf("%d\n",ans);
}
return 0;
}
4558



被折叠的 条评论
为什么被折叠?



