统计难题

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

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm

ba
b
band
abc

 
Sample Output
2 3 1 0
 
思路:字典树
AC代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Tree_Node
{
int cnt;
struct Tree_Node *child[];
}Node;
Node *root;
char str[];
void insert()
{
int i;
if(str == NULL)
return ;
char *p = str;
Node *t = root;
while(*p != '\0')
{
if(NULL == t -> child[*p- 'a'])
{
Node *temp = (Node *)malloc(sizeof(Node));
memset(temp, , sizeof(Node));
for(i = ;i < ;i ++)
{
temp -> child[i] = NULL;
temp -> cnt = ;
}
t -> child[*p - 'a'] = temp;
}
t = t -> child[*p - 'a'];
t -> cnt ++;
p ++;
}
}
int search()
{
if(NULL == root)
return ;
char *p = str;
Node *t = root;
while(*p != '\0')
{
if(NULL != t -> child[*p - 'a'])
{
t = t -> child[*p - 'a'];
p ++;
}
else
return ;
}
return t -> cnt;
} int main(int argc, char const *argv[])
{
int i;
char c[] = "abc";
Node TREEROOT;
root = &TREEROOT;
for(i = ;i < ;i ++)
{
root -> child[i] = NULL;
root -> cnt = ;
}
// freopen("in.c","r",stdin);
while(gets(str) && strcmp(str,""))
{
insert();
memset(str, , sizeof(str));
}
memset(str, , sizeof(str));
while(~scanf("%s", str))
{
printf("%d\n", search());
memset(str, , sizeof(str));
}
return ;
}

统计难题 HDOJ --1251的更多相关文章

  1. AC日记——统计难题 hdu 1251

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

  2. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

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

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

  4. hdoj 1251 统计难题 【字典树】

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

  5. hduoj 1251 统计难题

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

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

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

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

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

  8. HDU 1251 统计难题 (Trie)

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

  9. HDU 1251统计难题

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

随机推荐

  1. objective-c内存管理中autorelease的作用

    //创建自动释放池 @autoreleasepool { //autorelease会将对象放入自动释放池中,并返回该对象本身 //当自动释放池销毁时,将自动调用对象的release方法 Person ...

  2. Java文件操作 读写操作

    一.Java读取文件 案例1:读取D盘的1.txt文件 编码: File file = new File("D:/1.txt"); FileReader fr = new File ...

  3. UIView-图层方法

    // // ViewController.m // UIView-图层概念 // // Created by wangtouwang on 15/5/5. // Copyright (c) 2015年 ...

  4. Sumbline编译Less

    Less教程 Sublime Text 2的Less2Css插件介绍与安装 网址 http://fdream.net/blog/article/783.aspx 如果出现node.exe不是内部命令的 ...

  5. 在Linux下不使用密码远程登陆其他Linux

    有时需要再一台Linux上登陆其他Linux服务器,通常可以直接使用SSH命令,加入两台服务器一台服务器A,IP地址192.168.1.2,另一台服务器B,IP地址192.168.1.3,如果想从A服 ...

  6. 表格table样式布局设置

    <style> table{ border-collapse:collapse; margin:0 auto;} table tr td{ border:1px solid #000; l ...

  7. about hadoop-eclipse-plugin used by IDE

    Apache Hadoop Development Tools (HDT) is still in development phase. So, no official distribution of ...

  8. python 中对list做减法操作

    问题描述:假设我有这样两个list,          一个是list1,list1 = [1, 2, 3, 4, 5]          一个是list2,list2 = [1, 4, 5]     ...

  9. b+树 b-树的区别

    B+树与B*树小结 一.B+树 1.B+树定义与特性 B+树是B-树的变体,也是一种多路搜索树: 其定义基本与B-树同,除了: 1).非叶子结点的子树指针与关键字个数相同: 2).非叶子结点的子树指针 ...

  10. zzuli oj 1145 有问题的里程表 2

    Description 某辆汽车有一个里程表,该里程表可以显示一个整数,为该车走过的公里数.然而这个里程表有个毛病:它总是从3变到5,而跳过数字4,里程表所有位(个位. 十位.百位等)上的数字都是如此 ...