随笔分类 - Trie树、Trie图、DFA、NFA
摘要:赤裸裸的DFA,直接上模板了,可以交上去居然WA。仔细调了调,发现是原来自己写的模板有问题。之前写DFA模板的时候没有考虑到模式串会有重复并且还需要都统计的情况。大概改了改,能过这题了,但是代码改得挺乱,改天再整理整理吧。/* * hdu2222/win.cpp * Created on: 2013-1-6 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <
阅读全文
摘要:这题本来以为得DFA的,仔细审题以后发现,题目说每个关键字前4个字母不同(这意味着每个关键字结束的节点都是不同的),而且长度不超过60,马上醒悟,只要用所有的关键字建一棵Trie树,然后把主串跑一遍就可以了,复杂度为主串长度*60,肯定能过啊。。。/* * hdu1277/win.cpp * Created on: 2012-11-6 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>
阅读全文
摘要:还是赤裸裸的DFA,第一次交居然MLE,正苦恼时,看了一眼Discuss,有人说他把数据大小开成80000就过了,我一改,果然过了,汗啊,我之前开的是100000~/* * hdu2896/win.cpp * Created on: 2011-9-22 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAX_PAT
阅读全文
摘要:这题还是比较裸的,可是花了好长时间,一直TLE,最后加上一个优化,当一个字符放到DFA上跑不动的时候,后面相同的字符就直接PASS,终于过了,泪流满面啊……/* * hdu3695/win.cpp * Created on: 2011-9-21 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MAX_PATTERN
阅读全文
摘要:上次在北大暑期课期末考试里遇到这道题。最开始我是用BF实现DFA,超时。后来参考别人的程序用Trie实现,过了,但是还是理解得不够透彻。这几天做了不少KMP的题目,今天就用KMP实现了这个DFA,过了,很有成就感,呵呵~~/* * hdu3407/linux.cpp * Created on: 2011-7-29 * Author : ben */#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using
阅读全文
摘要:Trie树,题目不难,但还是WA了一次,没有考虑后输入的字符串是前输入的字符串的前缀的情况,真是太粗心了。不过做了几道Trie树的题目以后,代码写得还是比较通用了,慢慢再改进吧/* * hdu1671/win.c * Created on: 2011-8-19 * Author : ben */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#define CHAR_NUM 10#define BASE '0'typedef stru
阅读全文
摘要:题意思路简单,直接贴代码/* * poj2001/win.c * Created on: 2011-8-18 * Author : ben */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#define MAXN 1005#define MAXLEN 25int N;char strs[MAXN][MAXLEN];typedef struct Trie { int num; struct Trie * br[26];} Trie;Trie* ne
阅读全文
摘要:在网吧做题,赶上杭电OJ不能交题,先把代码存在博客里,回学校提交,有错再改。/* * hdu1251/win.c * Created on: 2011-8-18 * Author : ben */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>typedef struct Trie { int num; struct Trie * br[26];} Trie;Trie* newTrieNode() { int i; Trie* node = (Tri
阅读全文
浙公网安备 33010602011771号