随笔分类 - DS——Trie树
摘要:/*贡献出无数WA, 就因为一个地方写错, 悲剧的一晚上啊!不过能过了还是很爽的。 思路:用Trie树给字符串重新标号,然后用并查集,用一个a[i] 记录当前并查集跟结点上连接的结点数。*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ int flag; node * next[53];};const int N = 200005;int parent[N];int rank[N];int a[N];int num;node * n
阅读全文
摘要:字典树(Trie树),可以将之归为高级数据结构吧,黑书上把Trie树和线段树一起讲的。之所以叫它字典树,大概是因为它的结构太像字典了。就像这张图片:由字符串 abcd、abd、bcd、efg、hi。建成的Trie树如上图所示(Trie树大概的结构一看就能理解);Trie树的主要操作:1、建立新节点(姑且算是一样吧);2、插入(建树);3、查找(与树中的字符串匹配,这个过程是肯快的,如果能完全匹配的时间复杂度为是O(n),n = strlen(s)。)Trie的实现:1、结构体定义:struct node{ int flag; //标记 node * next[26]; ...
阅读全文
摘要:Trie水题,将每个单词插入Trie树,然后暴力拆分每个单词到树中查找匹配即可!有几个要注意的细节,代码注释中都标上了。My Code:#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX = 50007;struct node{ int flag; node * next[26];};char dis[MAX][30];node * newnode(){ int i; node * p = new node; p->flag =
阅读全文
摘要:题意:这题很好理解,给一个字典,上边有火星文对应的英语,让后输入一句话,把它翻译成英语。 思路:定义Tire树时加一个存放字符串的数组s[],按字典上的火星文建立Trie树,在每一个火星文的末尾将对应的英文存到s[]中。查找时如果能在Trie树中找到,就将s[]对应的串取出来(至于怎么取,自己先想想。。。)分解输入的句子时注意细节。还有就是静态建立Trie树,我开始用动态,WA了。。。My Code:#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>us
阅读全文
摘要:这道题有很多种解法,可以用qsort,也可以用Trie树,Trie树做时需要开静态数组开辟空间,动态分配会TLE!在Discuss里看到两组数据,注意一下221122121NONO下面是Trie树做法,在做题之前被某人提醒静态数组不要开得太大,否则会MLE,结果我就悲剧的RE了一晚上!! 2780K110MSView Code #include <stdio.h>#include <string.h>typedef struct node{ int flag; struct node * next[10];}node;node num[500000];int x = 0
阅读全文
摘要:大家都知道,Trie树(又称字典树)是一种树型数据结构,用于保存大量的字符串。它的优点是:利用字符串的公共前缀来节约存储空间。相对来说,Trie树是一种比较简单的数据结构,比较易于理解。话说上帝是公平的,简单的东西是要付出相应的代价的!Trie树也有它的缺点,它的内存消耗非常大。下面介绍一个减小内存消耗的小技巧。 我们先看看这个题目:http://acm.sdut.edu.cn/judgeonline/showproblem?problem_id=1500 看看这段程序:#include <stdio.h>#include <string.h>#include <
阅读全文
摘要:Message FloodTime Limit:1500MS Memory Limit:65536KTotal Submit:284 Accepted:40DescriptionWell, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year&
阅读全文

浙公网安备 33010602011771号