统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 14434    Accepted Submission(s): 6219

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
Recommend
Ignatius.L
     字典树.....
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h> typedef struct trie
{
//由于只有小写字母a~z-->26
struct trie *child[];
int deep; //--->相似的程度
}Trie; /*作为一个头指针*/ Trie *root; void Insert(char *a)
{
int len,i;
Trie *current, *creatnew;
len=strlen(a);
if(len)
{
current=root;
for(i=;i<len;i++)
{
if(current->child[a[i]-'a']!=)
{
current=current->child[a[i]-'a'];
current->deep=current->deep+;
}
else
{
creatnew=(Trie *)malloc(sizeof(Trie));
for(int j=;j<;j++)
{
creatnew->child[j]=;
}
current->child[a[i]-'a']=creatnew;
current=creatnew;
current->deep=;
}
}
}
} int Triefind(char *a)
{
int i,len;
Trie *current;
len=strlen(a);
if(!len) return ;
current=root;
for(i=;i<len;i++)
{
if(current->child[a[i]-'a']!=)
{
current=current->child[a[i]-'a'];
}
else
return ;
}
return current->deep;
} int main()
{
int i;
char temp[];
root=(Trie *)malloc(sizeof(Trie));
for(i=;i<;i++)
{
root->child[i]=;
}
root->deep=;
while(gets(temp),strcmp(temp,""))
Insert(temp);
memset(temp,'\0',sizeof(temp));
while(~scanf("%s",temp))
printf("%d\n",Triefind(temp));
free(root);
return ;
}

模板:

代码:

 /*hdu 1251 字典树之统计*/
#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct Trie
{
struct Trie *child[];
int deep;
}trie; int idx(char s){
return s-'a';
}
void Insert(char *s,trie *root)
{
trie *cur,*newcur;
cur=root;
int i,j;
for(i=;s[i]!='\0';i++)
{
if(cur->child[idx(s[i])]==NULL) //为空指针
{
newcur=(trie *)malloc(sizeof(trie));
newcur->deep=;
for(j=;j<;j++)
newcur->child[j]=NULL; //设置为空指针
cur->child[idx(s[i])]=newcur;
}
cur=cur->child[idx(s[i])]; //向下推一层
cur->deep+=; //层数加一
}
}
int query(char *s,trie *root)
{
trie *cur=root;
int i;
for(i=;s[i]!='\0';i++){
if(cur->child[idx(s[i])]!=NULL)
cur=cur->child[idx(s[i])];
else
return ;
}
int tem=cur->deep;
return tem;
}
void del(trie *root)
{
int i=;
for(i=;i<;i++){
if(root->child[i]!=NULL)
del(root->child[i]);
}
free(root);
}
char ss[];
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
trie *root=(trie *)malloc(sizeof(trie));
for(int i=;i<;i++)
root->child[i]=NULL; //设置为空指针
while(gets(ss),strcmp(ss,""))
Insert(ss,root);
while(scanf("%s",ss)!=EOF)
printf("%d\n",query(ss,root));
del(root);
return ;
}

HDUOJ-----(1251)统计难题的更多相关文章

  1. hduoj 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  2. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. HDU 1251 统计难题(Trie模版题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  6. HDU 1251统计难题

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  7. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  9. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

  10. HDU 1251 统计难题 (字符串-Trie树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  2. C++ 竞赛常用头文件

    C.传统 C++ #include <assert.h> 设定插入点 #include <ctype.h> 字符处理 #include <errno.h> 定义错误 ...

  3. 【BZOJ】【2502】清理雪道

    网络流/上下界网络流 带下界的最小可行流…… 我SB了,跑网络流的时候是得从虚拟源0往出跑……而不是S…… Orz Hzwer /*********************************** ...

  4. hadoop基准測试

    写測试hadoop jarhadoop-0.20.2-test.jar TestDFSIO -write -nrFiles 10 -fileSize 1000 ----- TestDFSIO ---- ...

  5. Informatica 常用组件Aggregator之二 分组依据端口

    聚合转换允许您为聚合定义组,而不是在所有的输入数据间执行聚合.例如,您可以查找按地区分组的总销量,而不是查找总的公司销量. 要为聚合表达式定义组,请选择聚合转换中的相应输入.输入/输出.输出和变量端口 ...

  6. Ubuntu12.04 64bit 下安装VNC server

    1. 安装gonme核心包(如果是字符界面的话) apt-get install x-window-system-coreapt-get install gnome-core (下载完成后需要安装dg ...

  7. cookie.setPath()的用法

    正常的cookie只能在一个应用中共享,即一个cookie只能由创建它的应用获得.1.可在同一应用服务器内共享方法:设置cookie.setPath("/");    本机tomc ...

  8. 【nodejs】使用put方式向后端提交数据

    页面代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...

  9. 【Window OS】”对于目标文件系统,文件XXXXX过大“导致无法进行文件操作的解决方法

    问题原因:这是目标文件系统不支持这么大的文件的操作问题.例如:目标文件系统的格式是FAT32,FAT32最大支持4G,如果你要进行发送或粘贴4G以上的文件就会出现这个问题. 解决办法:把目标文件系统的 ...

  10. Inception in CNN

    之前也写过GoogLeNet的笔记.但那个时候对Inception有些似懂非懂,这周又一次看了一遍,觉得有了新的体会,特地又一次写一篇博客与它再续前缘. 本文属于论文笔记性质.特此声明. Networ ...