hdu 1251 统计
他妹的。敲完了。电脑死机了,所有消失了,又从新打了一遍,。。。
这是什么节奏
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ZERO 0
#define ALPH_LEN 26 /* 26个字母 */
const char FIRST_CHAR = 'a'; typedef struct node
{
struct node *child[ALPH_LEN]; /* 存储下一个字符 */
int n; /* 记录当前单词出现的次数 */
}node, *Node; Node root; /* 字典树的根结点(不存储不论什么字符) */
/* 插入单词 */
void insert(char *str)
{
int i, index, len;
Node current = NULL, newnode = NULL; len = strlen(str); current = root; /* 開始时当前的结点为根结点 */
for (i = 0; i < len; i++) /* 逐个字符插入 */
{
index = str[i] - FIRST_CHAR; /* 获取此字符的下标 */
if (current->child[index] != NULL) /* 字符已在字典树中 */
{
current = current->child[index]; /* 改动当前的结点位置 */
(current->n)++; /* 当前单词又出现一次, 累加 */
}
else /* 此字符还没出现过, 则新增结点 */
{
newnode = (Node)calloc(1, sizeof(node)); /* 新增一结点, 并初始化 */
current->child[index] = newnode;
current = newnode; /* 改动当前的结点的位置 */
current->n = 1; /* 此新单词出现一次 */
}
}
}
/* 在字典树中查找单词 */
int find_word(char *str)
{
int i, index, len;
Node current = NULL; len = strlen(str); current = root; /* 查找从根结点開始 */
for (i = 0; i < len; i++)
{
index = str[i] - FIRST_CHAR; /* 获取此字符的下标 */
if (current->child[index] != NULL) /* 当前字符存在字典树中 */
{
current = current->child[index]; /* 改动当前结点的位置 */
}
else
{
return ZERO; /*还没比較完就出现不匹配, 字典树中没有此单词*/
}
} return current->n; /* 此单词出现的次数 */
} int main()
{
char tmp[11];
int i; root = (Node)calloc(1, sizeof(node)); while (gets(tmp), strcmp(tmp, "") != ZERO)
{
insert( tmp );
} while (scanf("%s", tmp) != EOF)
{
i = find_word( tmp );
printf("%d\n", i);
} return 0;
}
hdu 1251 统计的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
- HDU 1251 统计难题 (Trie)
pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/ ...
- hdu 1251 统计难题(trie 树的简单应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- HDU 1251 统计难题(字典树 裸题 链表做法)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- 字典树 HDU 1251 统计难题
;} 之前写的#include<iostream> #include<algorithm> #include<stdio.h> using namespace st ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- HDU 1251统计难题
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- RabbitMQ 关键词解释
源地址: https://www.cnblogs.com/hz04022016/p/6518138.html RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQ ...
- Android 图片设置圆角
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片 方法一: 通过第三方框架Glide实现图片显示有圆角,有三种写法如下: 1.1,第一种实现: RequestOptions ...
- 剑指Offer(书):打印从1到最大的n位数
题目:输入数字N,按顺序打印出从1到最大的N位十进制数,比如输入3,则打印出1.2.3一直到999 分析:N的范围不定.所以有可能超出范围,因此用数组存放以及输出.说实话,对复杂递归还是一头雾水 pu ...
- Android布局之线性布局——LinearLayout
本文将详细介绍线性布局的各种xml属性. xml属性 <?xml version="1.0" encoding="utf-8"?> <Line ...
- LeetCode (17)Letter Combinations of a Phone Number
题目 Given a digit string, return all possible letter combinations that the number could represent. A ...
- HDU 1087 Super Jumping! Jumping! Jumping! (LIS的最大和)
题意: 给定n个数的序列, 找出最长上升子序列和. 分析: #include<cstdio> #include<iostream> #include<queue> ...
- Web框架下安全漏洞的测试反思
此文已由作者王婷英授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在平时的测试中,一般情况下,我们都是比较关注功能业务测试,以及对应的接口测试,很少去关注对应的业务设计上存在 ...
- hdu 4710
#include<stdio.h> #include<math.h> __int64 min(__int64 a,__int64 b) { return a<b?a:b; ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- python学习之-- subprocess模块
subprocess 模块 功能:用来生成子进程,并可以通过管道连接它们的输入/输出/错误,以及获得它们的返回值.它用来代替多个旧模块和函数: os.system os.spawn* os.popen ...