随手练——HDU 1251 统计难题
知识点:前缀树。典型的前缀树模板。
这是用next[26]数组的版本,超内存了。(后来发现,用C++交不会超,G++就会超)
#include <iostream>
#include <malloc.h>
#include <string>
using namespace std;
typedef struct node{
int pass;
];
}
*trieTree;
trieTree init() {
trieTree t = (trieTree)malloc(sizeof(node));
; i < ; i++)t->next[i] = NULL;
t->pass = ;
return t;
}
void insert(trieTree T,string s) {
node *n = T;
; i < s.length(); i++) {
int index = s[i] - 'a';
if (T->next[index] == NULL) {
node *t = init();
T->next[index] = t;
}
T = T->next[index];
T->pass++;
}
}
int find(trieTree T, string s) {
node *n = T;
; i < s.length(); i++) {
int index = s[i] - 'a';
if (T->next[index] == NULL) {
return NULL;
}
T = T->next[index];
}
return T->pass;
}
int main() {
trieTree T = init();
string s;
while (getline(cin,s)) {
if (s.empty()) break;
insert(T, s);
}
while (getline(cin,s)) {
cout << find(T, s) << endl;
}
;
}
一开始过不了,我还想了半天,网上别人代码,也是next[26]的写法都能AC,我咋过不了???人丑就不给过???这个难受啊。
其实,next指针数组,浪费了很多空间,用STL map重新改了一下(malloc只分配内存,我改成了new),内存大约节省了25%~30(自己实现一个简单键值对,节省的空间会更多),C++、G++编译器都能AC了。
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef struct node{
int pass;
map<char,struct node *>m;
}
*trieTree;
trieTree init() {
trieTree t = new node;
t->pass = ;
return t;
}
void insert(trieTree T,string s) {
; i < s.length(); i++) {
if (T->m.find(s[i]) == T->m.end()) {
node *t = init();
T->m.insert(make_pair(s[i], t));
}
T = T->m[s[i]];
T->pass++;
}
}
int find(trieTree T, string s) {
node *n = T;
; i < s.length(); i++) {
if (T->m.find(s[i]) == T->m.end()) {
return NULL;
}
T = T->m[s[i]];
}
return T->pass;
}
int main() {
trieTree T = init();
string s;
while (getline(cin,s)) {
if (s.empty()) break;
insert(T, s);
}
while (getline(cin,s)) {
cout << find(T, s) << endl;
}
;
}
随手练——HDU 1251 统计难题的更多相关文章
- 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 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (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统计难题
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- HDU 1251 统计难题 (字符串-Trie树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 分布式时序数据库InfluxDB
我们内部的监控系统用到分布式时序数据库InfluxDB http://www.ttlsa.com/monitor-safe/monitor/distributed-time-series-databa ...
- 获取java根目录,加载根目录下的文件
就两句代码 String filepath = System.getProperty("user.dir")+"/a.xlsx"; File file=new ...
- HDU 4489(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4489 解题思路这里已经说的很清楚了: http://blog.csdn.net/bossup/article/d ...
- 使用NOPI读取Word、Excel文档内容
使用NOPI读取Excel的例子很多,读取Word的例子不多. Excel的解析方式有多中,可以使用ODBC查询,把Excel作为一个数据集对待.也可以使用文档结构模型的方式进行解析,即解析Workb ...
- java设计模式-----5、原型模式
原型(Prototype)模式是一种对象创建型模式,他采取复制原型对象的方法来创建对象的实例.使用原型模式创建的实例,具有与原型一样的数据. 原型模式的特点: 1.由原型对象自身创建目标对象.也就是说 ...
- 粗粒度权限控制(拦截是否登录、拦截用户名admin权限)
RBAC --> 基于角色的权限控制 tb_user tb_role tb_userrole tb_menu(增.删.改.查) tb_rolemenu 1 说明 给出三个页面:index ...
- BZOJ2987:Earthquake(类欧几里德算法)
Sol 设 \(n=\lfloor\frac{c}{a}\rfloor\) 问题转化为求 \[\sum_{i=0}^{n}\lfloor\frac{c-ax}{b}\rfloor+1=\sum_{i= ...
- latex 图形的放置
Next: 16.3 清除未处理的浮动图形 Up: 16. 浮动图形环境 Previous: 16.1 创建浮动图形 16.2 图形的放置 图形(figure)环境有一个可选参数项允许用户 ...
- 在GitHub上删除项目后,在Android Studio上传项目依然提示project is already on github
描述: 在GitHub上面上传项目,但是感觉有些问题,就想删除了重新上传. 但是在Android Studio重新上传项目时,遇到了问题,一直提示“project is already on gith ...
- zTree 学习笔记之(一)
zTree 学习笔记之(一) 简介 zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. 到底有哪些具体的优点,可以参见官网 ...