HDU - 1251 字典树模板题】的更多相关文章

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output对于每个提问,给出以该字符串为前缀的单词的数量. S…
这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ 只是记录下这道题学到的方法吧: for(rt = 0; *s; rt = nxt, ++s) { nxt=tree[rt][*s-tb]; if(!nxt) { nxt=tree[rt][*s-tb]=top; memset(tree[top],0,sizeof(tree[top])); top+…
1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b…
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; struct node { int num; node *next[maxn]; }; //字典树 class Tree{ public: node *head; //构造函数 Tree() { head…
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; ; struct Trie { ]; int cnt; //前缀数量 int ends; //单词数量,在本题中其实并没有用到…
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 $T$ 的前缀.输入字符串的总长度不超过 $10^6$,仅包含小写字母. 输入格式第一行两个整数 $N,M$.接下来 $N$ 行每行一个字符串 $S_i$.接下来 $M$ 行每行一个字符串表示询问. 输出格式对于每个询问,输出一个整数表示答案 样例输入3 2abbcabcabcefg 样例输出20…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词…
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include <cstring> #include <string> using namespace std; int main() { int i, len; ]; map<string, int> m; while( gets(str) ) { len = strlen(str); if…
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace std; ; struct node{ int num; node *next[maxn]; }; //字典树 class Tree{ public: node *head; Tree(){ head = New(); } node* New(){ node *p = new node(); ; i <…
题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树,从入门题开始.用数组实现,count数组表示每个结点出现次数,trie[0]为根节点.插入和查询一个字符串的复杂度为O(len).len为字符串的长度. AC code: #include<cstdio> #include<algorithm> #include<cstring&…
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就是一篇小文章.每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束. Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数. Sample Input y…
题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5 Trie树的基本实现 字母树的插入(Insert).删除( Delete)和查找(Find)都非常简单,用一个一重循环即可,即第i 次循环找到前i 个字母所对应的子树,然后进行相应的操作.实现这棵字母树,我们用最常见的数组保存(静态开辟内存)即可,当然也可以开动态的指针类型(动态开辟内存).至于结点对儿子的指向,一般有三种方法: 1.对每个结点开一个字…
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers: Emergency 911 Alice 97 625 999 Bob 91 12 54 26 In this case, it's not possible to c…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <queue> #include <vector> #define maxn 230050 #define lso…
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6951    Accepted Submission(s): 2214 Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The…
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<queue> #include<algorithm> #includ…
#include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void insert(char *s,int cur) { if(*s){ if(!node[cur].next[*s-'a']) node[cur].next[*s-'a']=++st; insert(s+,node[cur].next[*s-'a']); } else strcpy(node[cur].eng,en)…
Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaaaa'的时候可以只叫'aaa',因为没有第二个人名字的前三个字母是'aaa'.不过你不能叫'a',因为有两个人的名字都以'a'开头.村里的人都很聪明,他们总是用最短的称呼叫人.输入保证村里不会有一个人的名字是另外一个人名字的前缀(作为推论,任意两个人的名字都不会相同). 如果村里的某个人要叫所有人的…
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数进行建树,然后在查找的时候,利用贪心的策略,优先寻找与当前位数的0.1值不同的路线,从而达到异或值最大的目的. #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n,m,pos; ll val[N*]…
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).   Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束…
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<stdio.h> #include<iostream> using namespace std; struct tire_node { int count;//前缀出现的次数 tire_node* next[27];//指针数组,装指针 bool exit;//是否构成单词 tire_node…
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook.…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, 代码略长 #include <iostream> #include <stdio.h> #include <string.h> using namespace std; ; ; char str[MAX]; struct node{ int count; node *ne…
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. Input 第一行两个整数N和P(1≤P≤100000…
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给你一个插入的序列,问你最后AVL树的根节点是多少 AVL树模板题如果会AVL树,那么就是水题如果不会的话,那么就是难题我刚开始也不会,所以根本写不出来AVL树...后来花了一些时间学习了下,感觉网上很多模板都是用class写的,太麻烦了模板就是应该要…
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询区间,求最值.求和等,二维就是先切割第一维的区间,再去切割第二维的区间. code #include<bits/stdc++.h> using namespace std; #define lson l, m, rt << 1 #define rson m + 1, r, rt <…
Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu…
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; ]; ],Left[],Right[]; ]; v…
题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个串拆成len个串,然后map容器记录下就行了,不想存也 行,最后迭代一下也能出来),回来说字典树,就是最简单的前缀判断应用,我写的结构体里面有3个量,next[], v(出现次数) ,mk (是不是某个字符串的最后一位), 在查找的时候如果我们碰到了已经mk的,那么就直接发现前缀了,某个字符串是当前…
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 算法: 字典树 提示: 这题压要用c++提交,G++会超内存 *******************************************************/ #include<cstdio> #include<cstr…