poj 3294 Life Forms (后缀数组)
摘要:http://poj.org/problem?id=3294 依旧二分判定,只是要先找出最大长度,然后在根据这个长度再判定输出。对于长度相同的,用tag标记以免重复。code:#include<cstdio>//最长公共子串#include<cstring>#defineMax(a,b)a>b?a:bconstintmaxn=101001;intwa[maxn],wb[maxn],wv[maxn],ws[maxn],rank[maxn],height[maxn],sa[maxn],s[maxn],loc[maxn];boolvis[1001];charstr[10
阅读全文
posted @
2012-04-22 18:17
追逐.
阅读(321)
推荐(0)
poj 1226 Substrings (后缀数组)
摘要:http://poj.org/problem?id=1226 求给定的字符串及其反串的最大公共子串。 后缀数组,将所有的字符串及其子串连接起来,用分隔符隔开,并给每个字符串及反串的字符标记所在串。二分答案,看是否有n个字符串或者其反串存在长为mid的公共前缀。code:#include<cstdio>#include<cstring>#defineMax(a,b)a>b?a:bconstintmaxn=10001;intwa[maxn],wb[maxn],wv[maxn],ws[maxn],rank[maxn],height[maxn],sa[maxn],s[ma
阅读全文
posted @
2012-04-21 12:00
追逐.
阅读(222)
推荐(0)
poj 1743 Musical Theme (后缀数组)
摘要:http://poj.org/problem?id=1743 后缀数组的简单应用。求不重叠最长重复子串。在求出sa和height数组后,二分答案,看是否存在一个区间,其height>mid且abs(sa[i]-sa[j])>=mid,即i到j的公共前缀长度大于mid,且两后缀的首字母间距不小于mid(不重叠)。#include<cstring>#defineMax(a,b)a>b?a:bconstintmaxn=20001;intwa[maxn],wb[maxn],wv[maxn],ws[maxn],rank[maxn],height[maxn],sa[maxn]
阅读全文
posted @
2012-04-20 14:08
追逐.
阅读(180)
推荐(0)
poj 3080 Blue Jeans (后缀数组)
摘要:http://poj.org/problem?id=3080 还是和3450一样的。code:#include<cstdio>//最长公共子串#include<cstring>#defineMax(a,b)a>b?a:bconstintmaxn=650;intwa[maxn],wb[maxn],wv[maxn],ws[maxn],rank[maxn],height[maxn],sa[maxn],s[maxn],loc[maxn];charstr[61],ans[101];boolvis[101];intm;intcmp(int*r,inta,intb,intl){
阅读全文
posted @
2012-04-10 22:16
追逐.
阅读(234)
推荐(0)
poj 3450 Corporate Identity (后缀数组)
摘要:http://poj.org/problem?id=3450 假期做过这题,当时是用KMP做的,这次用后缀数组。 把n个字符串连接起来,中间用不同的且输入串中不会出现的字符隔开,对新生成串的每个字符根据所在原子串划分区域,用Loc数组标记。 二分公共子串长度,查找相邻的后缀中是否有连续n个后缀的height值大于枚举值,且分属于n个不同的串。code:#include<cstdio>//最长公共子串#include<cstring>#defineMax(a,b)a>b?a:bconstintmaxn=805000;intwa[maxn],wb[maxn],wv[m
阅读全文
posted @
2012-04-10 21:21
追逐.
阅读(1029)
推荐(0)
bcl 1387 最长重复子串 (后缀数组)
摘要:http://www.bianchengla.com/practise/problem?id=1387 第一道后缀数组,求出最大的height值即可。更多的是当个模板用吧。code:#include<cstdio>//最长重复子串#include<cstring>constintmaxn=10001;intwa[maxn],wb[maxn],wv[maxn],ws[maxn],rank[maxn],height[maxn],sa[maxn],s[maxn];charstr[maxn];intcmp(int*r,inta,intb,intl){returnr[a]==r[
阅读全文
posted @
2012-04-03 19:25
追逐.
阅读(271)
推荐(0)
hdu 3065 病毒侵袭持续中 (AC)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3065 AC自动机,主要注意的就是两个特征码的重叠情况。code:#include<iostream>#include<cstring>#include<cstdio>usingnamespacestd;constintkind=30;structnode{node*fail;node*next[kind];intcount;intid;node(){fail=NULL;count=0;id=-1;memset(next,NULL,sizeof(next));}}*q[50
阅读全文
posted @
2012-02-22 01:26
追逐.
阅读(245)
推荐(0)
hdu 2896 病毒侵袭 (AC)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2896 这题WA了好多好多次,大概8、9次吧,从cf进行到一半就开始在这调。经过不懈的努力终于发现,TMD,vis的memset放错地方了!!!这都什么事啊,老被这种错给整的那么郁闷。。。code:#include<iostream>#include<cstring>#include<cstdio>usingnamespacestd;constintkind=130;structnode{node*fail;node*next[kind];intcount;intid;no
阅读全文
posted @
2012-02-21 01:56
追逐.
阅读(223)
推荐(0)
poj 2503 Babelfish (trie)
摘要:http://poj.org/problem?id=2503 思路很简单,对foreign language word建trie树,插入时一并传入word的序号,与english word相对应。查找时直接查找word标记的序号,从存储数组中输出englishword就好了。 比较蛋疼的是这题的输入,一开始真心搞不定啊。。搞定后直接1Y。code:#include<cstdio>#include<cstring>charstr[100001][11];//模式串charestr[100001][11];#defineMAX26//字符集大小typedefstructTr
阅读全文
posted @
2012-02-07 21:54
追逐.
阅读(200)
推荐(0)
poj 2001 Shortest Prefixes (trie)
摘要:拿到这题,大致的画了画图,发现只要标记好每个节点出现的次数就好了。 插入时,若字符存在,n++,若不存在建立新节点,他本身也为自己的前缀。 查找时,若next[k]->nCount!=1,说明其子节点有多个,无法确定唯一,则输出该字符,若nCount=1,则可以结束查找了。code:#include<cstdio>#include<cstring>charstr[1001][21];#defineMAX30//字符集大小typedefstructTrieNode{intnCount;//记录该字符出现次数structTrieNode*next[MAX];}Trie
阅读全文
posted @
2012-02-07 19:54
追逐.
阅读(141)
推荐(0)
poj 3450 Corporate Identity (KMP)
摘要:http://poj.org/problem?id=3450 沾3080的光,这题让我WA了N多次!看到两个题一样,买一送二啊,上手就做,悲剧了。。 主要是没考虑一个字符时的情况,改来改去,最后终于想起来把KMP中的m初始化成-1就OK了。我这脑子是锈掉了么?忙活3个小时最后改了一个数就行了,纠结啊!code:#include<cstdio>#include<cstring>intnext[201];charsubstr[201];charstr[4001][201];charresult[201];chartemp[201];intlen[4001];intn,max
阅读全文
posted @
2012-02-06 00:06
追逐.
阅读(385)
推荐(0)
poj 1936 All in All
摘要:http://poj.org/problem?id=1936 额,这个用到啥了?好像只有字符串以'\0'为结尾吧...code:#include<cstdio>charstr[100001];charsubstr[100001];intmain(){inti,j;while(~scanf("%s%s",substr,str)){i=0,j=0;while(substr[i]!='\0'&&str[j]!='\0'){if(substr[i]==str[j])i++;j++;}if(substr[i]
阅读全文
posted @
2012-02-05 19:19
追逐.
阅读(134)
推荐(0)
poj 3080 Blue Jeans (KMP)
摘要:http://poj.org/problem?id=3080 给定n个字符串,求最长公共子串。 利用KMP是对子串从1..j匹配母串,枚举第一个母串的所有后缀子串,在KMP匹配过程中得到j的最大值(这里不能完全匹配后再得最大值,为了确保取到所有子串),对n-1个母串匹配完成后,j最大值的最小值便为公共子串。若有多个这样的子串,字典顺序小者为result。 这里用到了string的两个函数,strcpy(substr, str)和strncpy(substr, str, n)。前者是复制到空字符停止复制,后者则是先找到n个字符再开始复制。使用后者后,要对复制所得字符串封存,substr[n] =
阅读全文
posted @
2012-02-05 19:02
追逐.
阅读(387)
推荐(0)
poj 3461 Oulipo (KMP)
摘要:http://poj.org/problem?id=3461 基础KMP,要注意一次查找完成后,到下一可查找处继续匹配,这样才能保证得到最终个数。code:#include<cstdio>#include<cstring>charsubstr[10001];charstr[1000001];intnext[10001];intsublen,len,ans;voidget_next(){next[1]=0;intj=0;for(inti=2;i<=sublen;i++){while(j>0&&substr[j+1]!=substr[i])j=n
阅读全文
posted @
2012-02-05 17:06
追逐.
阅读(211)
推荐(0)
poj 2752 Seek the Name (KMP)
摘要:http://poj.org/problem?id=2752 KMP的next数组应用。一句话,next[j]必须为满足str[1..next[j]] = str[j-next[j]+1..j]的最大值。 以abababa为例,next[7] = 5,则str[1..5] = str[3..7],显然str[3..5] = str[5..7]。 next[5] = 3,则 str[1..3] = str[3..5],根据上面所得可知str[1..3] = str[5..7]。 由于next数组的性质,显然上述对任意情况都是成立的,每个next保存的值都可使str[1..next[j]...
阅读全文
posted @
2012-02-05 03:10
追逐.
阅读(184)
推荐(0)
poj 2406 && poj 1961 (KMP)
摘要:poj2406 http://poj.org/problem?id=2406 给定一个字符串,问最多是多少个相同子串不重叠连接构成。 KMP的next数组应用。这里主要是如何判断是否有这样的子串,和子串的个数。 next数组的作用是在匹配无法进行下去时,可继续匹配的位置。这就要求next[j]必须为满足str[1..next[j]] = str[j-next[j]+1..j]的最大值。对于串的每一个前缀都取满足条件的最大值,则next[len]的值即为前一个子串的尾位置。 例如ababab,next[4]的值为2,即指向第一个b,因为str[1..2] = str[3..4],同理next[6
阅读全文
posted @
2012-02-04 22:08
追逐.
阅读(357)
推荐(0)
AC模版
摘要:hdu 2222:#include<cstdio>#include<cstring>#definemm88888#definemn55#defineN26inttire[mm][N];intfail[mm],w[mm],Q[mm];intcg[128];intsize;chartmp[mn],s[1111111];voidbuild(char*word){inti=0,j;for(;*word;++word,i=tire[i][j])if(!tire[i][j=cg[*word]]){memset(tire[size],0,sizeof(tire[size]));w[t
阅读全文
posted @
2011-11-20 17:56
追逐.
阅读(178)
推荐(0)
hdu 1251 前缀树
摘要:题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 普通trie树,只是在构建的时候统计出每个节点儿子的个数,最后直接匹配每个前缀,输出最后一个前缀字符在树中儿子的个数加一(其本身)即可。 纠结的是题目没给数据范围,tree数组开100010 RE,开200010 还是RE,一狠心开了500010,果断AC。。。代码:#include<iostream>#include<cstring>#include<cstdio>usingnamespacestd;intnum;intans;structnode{in
阅读全文
posted @
2011-10-13 19:47
追逐.
阅读(145)
推荐(0)
poj 3630(Phone List)
摘要:题目链接:http://poj.org/problem?id=3630 单纯的trie树。code:#include<cstring>#include<cstdio>charstr[100];//模式串#defineMAX26//字符集大小typedefstructTrieNode{intcount;//记录该字符出现次数boolf;structTrieNode*next[MAX];}TrieNode;TrieNodeMemory[1000000];intallocp=0;/*初始化*/voidInitTrieRoot(TrieNode**pRoot){*pRoot=N
阅读全文
posted @
2011-10-12 22:06
追逐.
阅读(193)
推荐(0)
Trie树详解(转)
摘要:trie树——字典树 详细讲解!!又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高。字典树与字典很相似,当你要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典树里没有该单词,如果在就在该字母的孩子节点里找是不是有单词的第二个字母,没有说明没有该单词,有的话用同样的方法继续查找.字典树不仅可以用来储存字母,也可以储存数字等其它数据。Trie
阅读全文
posted @
2011-10-12 11:41
追逐.
阅读(532)
推荐(0)