随笔分类 -  【AC自动机】

【总结】AC自动机
摘要:给出n个单词,再给出一段包含m个字符的文章,找出有多少个单词在文章里出现过。1、对n个单词构造字典树。2、构造失败指针。设当前节点为X,失败指针指向Y。1。若当前节点X没有儿子t,则X的儿子t等价于Y的儿子t。2。若当前节点X有儿子t,t的失败指针指向Y的儿子t。Y与X有最长公共后缀。3、模式匹配。沿着next指针遍历。例:4个单词:0101、1011、1100、0010。在构造完失败指针后,顺便完善next指针,使得匹配时只需沿着next指针遍历,匹配时沿着fail指针遍历是为了统计出当前串包含的子串。模板题:【HDU】2222 Keywords Search【HDU】2896 病毒侵袭【H 阅读全文
posted @ 2012-08-09 18:24 DrunBee 阅读(3426) 评论(0) 推荐(1)
【ZOJ】3494 BCD Code
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MOD 1000000009 5 #define MAXN 2010 6 #define MAXM 210 7 using namespace std; 8 struct Trie { 9 bool end; 10 int fail, next[2]; 11 void Init() { 12 end = false; 13 fail = 0; 14 memset(next... 阅读全文
posted @ 2012-08-09 01:05 DrunBee 阅读(480) 评论(0) 推荐(0)
【HDU】3247 Resource Archiver
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 60000 5 #define MAXL (1<<10) 6 #define MAXM 200 7 using namespace std; 8 struct Trie { 9 bool virus; 10 int end, fail, next[2]; 11 void Init() { 12 virus = false; 13 end = fail = next[0] = n... 阅读全文
posted @ 2012-08-07 23:13 DrunBee 阅读(584) 评论(0) 推荐(0)
【ZOJ】3228 Searching the String
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 100010 5 #define MAXM 26 6 #define INF 123456789 7 using namespace std; 8 struct Trie { 9 int fail, pos[2], cnt[2], len, next[MAXM]; 10 void Init() { 11 len = fail = pos[0] = pos[1] = cnt[0] = cnt[1] = ... 阅读全文
posted @ 2012-08-07 00:30 DrunBee 阅读(596) 评论(0) 推荐(0)
【HDU】3341 Lost's revenge
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 15000 5 #define MAXM 510 6 using namespace std; 7 int size, K[5], dp[MAXN][MAXM]; 8 bool vis[MAXN]; 9 char str[MAXM]; 10 struct Trie { 11 int cnt, fail, next[4]; 12 void Init() { 13 cnt = fail = 0; 14 ... 阅读全文
posted @ 2012-08-06 22:48 DrunBee 阅读(464) 评论(0) 推荐(0)
【HDU】2296 Ring
摘要:1 #include<iostream> 2 #include<string> 3 #include<queue> 4 #include<cstdio> 5 #include<cstring> 6 #define MAXM 26 7 #define MAXN 1010 8 #define INF 123456789 9 using namespace std; 10 char str[MAXN][MAXM]; 11 string path[MAXM << 1][MAXN]; 12 int size, dp[MAXM < 阅读全文
posted @ 2012-08-05 17:43 DrunBee 阅读(524) 评论(0) 推荐(0)
【HDU】2457 DNA repair
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 #define MAXN 1010 6 #define INF 123456789 7 using namespace std; 8 struct Trie { 9 int fail, next[4]; 10 bool end; 11 void Init() { 12 end = false; 13 fail = 0; 14 memset(ne... 阅读全文
posted @ 2012-08-05 13:46 DrunBee 阅读(450) 评论(0) 推荐(0)
【POJ】1625 Censored!
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXM 256 5 #define MAXN 550 6 using namespace std; 7 struct Trie { 8 bool end; 9 int fail, next[MAXM]; 10 void Init() { 11 end = false; 12 fail = 0; 13 memset(next, 0, sizeof(next)); 14 ... 阅读全文
posted @ 2012-08-05 12:37 DrunBee 阅读(446) 评论(0) 推荐(0)
【HDU】2825 Wireless Password
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 110 5 #define MAXM 26 6 #define MAXL (1<<10) 7 #define MOD 20090717 8 using namespace std; 9 char str[MAXN]; 10 int size, digit[MAXL], dp[30][MAXN][MAXL]; 11 struct node { 12 int fail, end, next[MAXM]; 13 阅读全文
posted @ 2012-08-04 11:23 DrunBee 阅读(478) 评论(0) 推荐(0)
【HDU】2243 考研路茫茫――单词情结
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 26 5 #define MOD 10330176681277348905LL 6 typedef unsigned long long LL; 7 using namespace std; 8 char str[MAXN]; 9 int size; 10 LL total, ans; 11 struct node { 12 int fail, next[MAXN]; 13 bool end; 14 void... 阅读全文
posted @ 2012-08-04 10:12 DrunBee 阅读(699) 评论(0) 推荐(1)
【POJ】2778 DNA Sequence
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 typedef long long LL; 5 #define MAXN 110 6 #define MOD 100000 7 using namespace std; 8 char str[MAXN]; 9 int size; 10 struct Matrix { 11 LL mat[MAXN][MAXN]; 12 void Zero() { 13 memset(mat, 0, sizeof(mat)); 14 } ... 阅读全文
posted @ 2012-08-04 00:48 DrunBee 阅读(507) 评论(0) 推荐(1)
【ZOJ】3430 Detect the Virus
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<queue> 5 #define MAXL 256 6 #define MAXN 50010 7 using namespace std; 8 char str[MAXN]; 9 int dg[MAXN], code[MAXN], size; 10 bool vis[MAXN]; 11 struct node { 12 int fail, cnt, next[MAXL]; 13 void Init() { 14 阅读全文
posted @ 2012-08-03 22:23 DrunBee 阅读(699) 评论(0) 推荐(0)
【HDU】3065 病毒侵袭持续中
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 2000010 5 #define MAXM 50000 6 #define MAXL 128 7 using namespace std; 8 char str[MAXN], dic[MAXM][60]; 9 int size, cnt[MAXM];10 struct node {11 int pos, fail, next[MAXL];12 void Init() {13 pos = fail = 0;14 .. 阅读全文
posted @ 2012-08-01 22:40 DrunBee 阅读(969) 评论(0) 推荐(0)
【HDU】2896 病毒侵袭
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 100010 5 #define MAXL 510 6 #define MAXM 128 7 using namespace std; 8 char str[MAXN]; 9 int size;10 bool vis[MAXL], flag;11 struct node {12 int pos, fail, next[MAXM];13 void Init() {14 pos = fail = 0;15 ... 阅读全文
posted @ 2012-08-01 21:53 DrunBee 阅读(583) 评论(0) 推荐(0)
【HDU】2222 Keywords Search
摘要:1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define MAXN 1000000 5 #define MAXM 26 6 using namespace std; 7 char str[MAXN]; 8 int size, ans; 9 struct node {10 int fail, cnt, next[MAXM];11 bool vis;12 void Init() {13 fail = cnt = 0;14 vis = false;15 ... 阅读全文
posted @ 2012-08-01 21:01 DrunBee 阅读(535) 评论(0) 推荐(0)