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 ...
随机推荐
- springmvc请求小例子
1.welcome.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- shiro配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- wps左侧显示目录
单击视图----文档结构图,在下拉选项中选择靠左即可,如图所示
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- 【java 理论篇 2】J2EE的13种规范
导读:看完了J2EE的视频,没有什么技术实践,现在就从理论上说明一下J2EE的13种规范,以及现在的自己对它的一个理解.可能会有偏差,但是,算是做为目前的一个记录. 一.13种规范 1.1.JDBC( ...
- NYOJ760-See LCS again,有技巧的暴力!
See LCS again 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...
- [HDU3065]病毒持续侵袭中(AC自动机)
传送门 AC自动机的又一模板,统计每个字符串在文本中的次数. 所以就不需要vis数组了. ——代码 #include <cstdio> #include <cstring> # ...
- [NOIP2003] 提高组 洛谷P1038 神经网络
题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...
- 使用Navicat进行数据库对比同步
使用Navicat进行数据库对比同步 当有多个数据库时,有时会出现结构或者数据不同步的问题,这时候可以使用navivat工具对比同步( 我的Navicat版本是11.0.17). 参考博客: 岁伏的博 ...
- ArcGIS engine中Display类库——Display
转自原文 ArcGIS engine中Display类库——Display Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这 ...