随手练——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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 完美世界-Java游戏开发-二面
时间:2017-03-30 时长:15分 类型:二面 面试官比较聊得来,人比较和善,游戏面试还是nice的,老铁 1. 自我介绍 2. 平时玩哪些游戏?端游.页游 3. Maven你是怎么使用的? 4 ...
- winform程序限制只能打开一个进程
有很多方案,先来最傻瓜式的 : static class Program { /// <summary> /// 应用程序的主入口点. ...
- 11、Map、可变参数、Collections
Map接口 Map集合概述 *A:Map集合概述: 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同 a:Collection中的集合,元素 ...
- 关于JAVA是值传递还是引用传递的问题
1.概念 值传递:方法调用时,实际传入的是它的副本,在方法中对值的修改,不影响调用者的值. 引用传递:方法调用时,实际传入的是参数的实际内存地址,调用者和调用方法所操作的参数都指向同一内存地址,所以方 ...
- 基于Spark GraphX计算二度关系
关系计算问题描述 二度关系是指用户与用户通过关注者为桥梁发现到的关注者之间的关系.目前微博通过二度关系实现了潜在用户的推荐.用户的一度关系包含了关注.好友两种类型,二度关系则得到关注的关注.关注的好友 ...
- sql: Oracle 11g create procedure
CREATE OR REPLACE PROCEDURE proc_Insert_BookKindList ( temTypeName nvarchar2, temParent int ) AS nco ...
- laravel开发之-安装汉化语言包
第一种方法: 1.输入命令:composer require "overtrue/laravel-lang:dev-master" 2.将config/app.php中命令“Ill ...
- 微信小程序开发6-WXSS
1.WXSS(WeiXin Style Sheets)是一套用于小程序的样式语言,用于描述WXML的组件样式,也就是视觉上的效果.WXSS与Web开发中的CSS类似.为了更适合小程序开发,WXSS对C ...
- Java基础之JSONObject的使用
private static JSONObject createJSONObject() { JSONObject jsonObject = new JSONObject(); jsonObject. ...
- int占几个字节?
class Program19 { static void Main(string[] args) { // true,或false Console.WriteLine("bool占用:&q ...