杭电1251--统计难题
2014-06-07 17:44
302 查看
Problem
Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
Sample Output
2
3
1
0
////////////////////////////////////////////////////////////////
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
struct node{
int n;
struct node *child[26];
};
struct node *root;
int m=sizeof(struct node);
struct node * Newnode()//申请节点地址
{
int i;
struct node *p;
p=(struct
node *)malloc(m);
for(i=0;i<26;i++)
p->child[i]=NULL;
return p;
}
void Insert(char s[])
{
int i,len;
struct
node *p,*q;
p=root;
len=strlen(s);
if(len==0)
return ;
for(i=0;i<len;i++)
{
if(p->child[s[i]-'a']!=NULL)
{
p=p->child[s[i]-'a'];
p->n++;
}
else
{
q=Newnode();
q->n=1;
p->child[s[i]-'a']=q;
p=q;
}
}
return ;
}int Search(char s[])
{
int i,len;
struct node *p;
p=root;
len=strlen(s);
if(len==0)
return 0;
for(i=0;i<len;i++)
{
if(p->child[s[i]-'a']!=NULL)
p=p->child[s[i]-'a'];
else
return 0;
}
return p->n;
}
int main()
{
int ans;
char s[12];
root=Newnode();
root->n=0;
while(gets(s),strcmp(s,"")!=0)
Insert(s);
while(gets(s))
{
ans=Search(s);
printf("%d\n",ans);
}
return 0;
}
Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
Sample Output
2
3
1
0
////////////////////////////////////////////////////////////////
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
struct node{
int n;
struct node *child[26];
};
struct node *root;
int m=sizeof(struct node);
struct node * Newnode()//申请节点地址
{
int i;
struct node *p;
p=(struct
node *)malloc(m);
for(i=0;i<26;i++)
p->child[i]=NULL;
return p;
}
void Insert(char s[])
{
int i,len;
struct
node *p,*q;
p=root;
len=strlen(s);
if(len==0)
return ;
for(i=0;i<len;i++)
{
if(p->child[s[i]-'a']!=NULL)
{
p=p->child[s[i]-'a'];
p->n++;
}
else
{
q=Newnode();
q->n=1;
p->child[s[i]-'a']=q;
p=q;
}
}
return ;
}int Search(char s[])
{
int i,len;
struct node *p;
p=root;
len=strlen(s);
if(len==0)
return 0;
for(i=0;i<len;i++)
{
if(p->child[s[i]-'a']!=NULL)
p=p->child[s[i]-'a'];
else
return 0;
}
return p->n;
}
int main()
{
int ans;
char s[12];
root=Newnode();
root->n=0;
while(gets(s),strcmp(s,"")!=0)
Insert(s);
while(gets(s))
{
ans=Search(s);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 杭电 HOJ 1251 统计难题 解题报告
- 【杭电】[1251]统计难题
- 杭电1251 统计难题(字典树)
- 杭电1251——统计难题(字典树的应用)
- 【字典树】杭电1251 : 统计难题
- 杭电1251统计难题
- 【字典树】杭电1251 : 统计难题
- 杭电OJ 1251 统计难题 字典树入门
- 杭电1251 统计难题 字典树的应用
- 杭电1251 统计难题
- 杭电 1251 统计难题
- HDU 1251 统计难题
- HDU 1251 ——统计难题(Trie 入门)
- HDU 1251 (统计难题) 字典树模板&&map实现
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题 Trie树的应用
- hdu 1251 统计难题 字典树
- hdu 1251 统计难题(字典树)
- poj 1251 统计难题(字典树)
- hdu 1251 统计难题 字典树