随手练——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近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
随机推荐
- 删除弹出提示框_MVC
<td> @Ajax.ActionLink(@shared.Delete, "DeleteServicetag", new { id = item.ID }, new ...
- 云主机安装Tomcat上传自己的网站
前几天在DigitalOcean上买一个云服务器(1g内存,1核,25gssd,1tb流量,一个月5$,按天收费),用github的students developer package里面的优惠码拿到 ...
- jQuery选择器,外加例子讲解
jQuery选择器的优势: (1) 代码更简单(2) 支持CSS1到CSS3选择器(3) 完善的处理机制 jQuery选择器的分类 (1) 基本选择器 基本选择器是jQuery中使用最多的选择器,它又 ...
- [JAVA IDEA]在使用maven项目中,无法读取resources文件夹中的配置文件的一种解决方案
1.在通过配置文件来连接数据库时,在resouces文件中放入了db.properties配置文件,但无法正常读取到 读取配置文件信息的代码: InputStream input=JdbcUtil.c ...
- HTTP 错误500.19 - 错误代码 0x80070021
1.错误描述 HTTP 错误500.19 -Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 Begi ...
- javascript之url转义escape()、encodeURI()和decodeURI(),ifram父子传参参数有中文时出现乱码
ifram父子传参参数有中文时出现乱码,可先在父级页面用encodeURI转义,在到子页面用进行decodeURI()解码 我们可以知道:escape()除了 ASCII 字母.数字和特定的符号外,对 ...
- 第二天-while循环 格式化输出 运算符 编码
一.while循环 while 条件: 语句块(循环体) #判断条件是否成立,若成立执行循环体,然后再次判断条件...直到不满足跳出循环 else: 当条件不成立的时候执行这里,和break没 ...
- SignalR实时通信
/// <summary> /// Message集线器类 /// </summary> [HubName("chathub")] public class ...
- C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序
基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...
- IDEA 的缓存问题
当IDEA还是使用以前的配置时,大概率是缓存问题,查看target,里面的内容就是编译好的东西,问题都是出自这里.