摘要: 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 追逐. 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 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 追逐. 阅读(372) 评论(0) 推荐(0) 编辑
摘要: 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 追逐. 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 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 追逐. 阅读(176) 评论(0) 推荐(0) 编辑