hdu_1251统计难题(字典树Trie)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 31479 Accepted Submission(s): 12087
注意:本题只有一组测试数据,处理到文件结束.
band
bee
absolute
acm
ba
b
band
abc
#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 26
typedef struct Trie {
int v;//根据需要变化
Trie *next[MAXN];
//next是表示每层有多少种类的数,如果只是小写字母,则26即可,
//若改为大小写字母,则是52,若再加上数字,则是62了
} Trie;
Trie root; void createTrie(char *str) {
int len = strlen(str);
Trie *p = &root, *q;
for(int i = ; i < len; i++) {
int id = str[i]-'a';
if(p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(root));
q->v = ;//初始v==1
for(int j = ; j < MAXN; j++)
q->next[j] = NULL;
p->next[id] = q;
p = p->next[id];
} else {
p->next[id]->v++;
p = p->next[id];
}
}
// p->v = -1;//若为结尾,则将v改成-1表示
} int findTrie(char *str) {
int len = strlen(str);
Trie *p = &root;
for(int i = ; i < len; i++) {
int id = str[i]-'a';
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return ;
// if(p->v == -1) //字符集中已有串是此串的前缀
// return -1;
}
return p->v;
//return -1; //此串是字符集中某串的前缀
}
int dealTrie(Trie* T) {
//动态字典树,有时会超内存,这是就要记得释放空间了
if(T==NULL)
return ;
for(int i = ; i < MAXN; i++) {
if(T->next[i]!=NULL)
dealTrie(T->next[i]);
}
free(T);
return ;
}
int main() {
char str[];
for(int i = ; i < MAXN; i++)
root.next[i] = NULL;
while(gets(str) && str[]!='\0')
createTrie(str);
memset(str, , sizeof(str));
while(scanf("%s", str) != EOF) {
int ans = findTrie(str);
printf("%d\n", ans);
}
return ;
}
set方法:
#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
#include <string>
using namespace std; int main()
{
char str[];
map<string, int> m;
while(gets(str))
{
int len = strlen(str);
if (!len)
{
break;
}
for(int i = len; i > ; i--)
{
str[i] = '\0';
m[str]++;
}
}
while(gets(str))
{
cout<<m[str]<<endl;
} return ;
}
hdu_1251统计难题(字典树Trie)的更多相关文章
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- 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 Subm ...
- HDU-1251 统计难题,字典树或者map!
统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdoj1251 统计难题 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
随机推荐
- ucore lab1练习2 qemu+gdb 不能协作调试的问题make lab1-mon
本练习是qemu结合gdb调试,但是我做实验的时候并不能像视频输入make lab1-mon那样顺利调试,期间有各种error,后来我找到原因,请看解决方法. 请先把ucore_lab文件删除,以下全 ...
- xamarin android alertdialog详解
说明一下:学习xamarin android一段时间,准备写一些xamarin android相关的例子,alertdialog也是使用的非常多得空间之一,非常感谢鸟巢上的小猪,我也是看着他写的教程学 ...
- php-递归创建级联目录
方法一: function mk_dir($path_arr,$root){ if(!empty($path_arr)){ static $path;//每次保存上次调用的值 $path .= '/' ...
- vbs的一些入门基础。。。
VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言. Microsoft Visual Basic是微软公司出品的一套可视化编程工具, 语法基于Basic. 脚本语言, 就 ...
- install pytorch
1. install and update pip3 2. install numpy and scipy 3. install pytorch
- windows上安装redis
The Redis project does not officially support Windows. However, the Microsoft Open Tech group develo ...
- react看这篇就够了(react+webpack+redux+reactRouter+sass)
本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fe ...
- 妙味课堂:JavaScript初级--第11课:字符串、查找高亮显示
1.数字字母 Unicode 编码 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content- ...
- amaze UI 笔记 - JS
导航添加依据 http://amazeui.org/javascript 下面内容属学习笔记,如有理解偏差和错误请留言相告,感谢!* =(官网这块写的很详细) 一 .UI增强 1.警告框 显示可关闭的 ...
- css布局--水平垂直居中
1. 使用text-align 和 vertical-align 和 inline-block实现水平垂直居中 html <div class="parent"> &l ...