随笔分类 -  KMP

摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2087View Code #include<iostream>using namespace std;int main(){ char a[1001],b[1001]; int i,j; while(cin>>a) { if(a[0]=='#') break; cin>>b; int L1=strlen(a); int L2=strlen(b); int sign=0 , flag=0; i=sign,j=0; while(i<L1) { if(a[i 阅读全文
posted @ 2011-05-16 21:04 聊聊IT那些事 阅读(513) 评论(0) 推荐(0)
摘要:View Code /*Poj2406题解:if(len%(len-next[len])==0),则重复子串的长度为 len-next[len].证明如下,next[len]表示到len为止,满足既是前缀子串又是后缀子串的最长长度,如下图{a,c} = {b,d}若len%(len – next[len])==0,则说明{ a ,b} 的长度可以被{a, d} 的长度整除,同样可以被{b,c}的长度整除。而此时,由于{b,c}是{b,d}的前缀子串又是后缀子串且保持{a,b}的长度可以被{a,c}整除。这样,{a,c}就相当于原来的{a,d},而{b,c}就相当于原来的{b,d},通过这样的划 阅读全文
posted @ 2011-05-16 20:10 聊聊IT那些事 阅读(665) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2752View Code //寻找前子串与后子串相等的子串#include<iostream>using namespace std;char ch[400001];int next[400001];int ans[400001];void solve (char *s, int ls){ int i=0,j=-1; next[0] = -1; while(i<ls) { if(j==-1 || s[i]==s[j]) { i++; j++; next[i] = j; } else { j = next[j]; } }}i 阅读全文
posted @ 2011-05-16 20:08 聊聊IT那些事 阅读(215) 评论(0) 推荐(0)
摘要:字符串查找Time Limit:1000MS Memory Limit:65536KTotal Submit:73 Accepted:24 Description 请编制程序实现下面的功能:统计一个子字符串在另一个字符串中出现的次数。Input 有多组数据,每组两行,两行输入的都是字符串。 字符串中字符没有限制,如第一行第一个字符是#时表示输入结束。Output 每组只输出一个整数,其单独成行,该整数代表第二个字符串在第一个字符串中出现的次数。Sample Input asd asasdfg asd as zx67 asd mkloas#Sample Output 6Hint 输入数据的格式是 阅读全文
posted @ 2011-05-15 20:54 聊聊IT那些事 阅读(298) 评论(0) 推荐(0)
摘要:View Code void get_next(){ int i=-1,j=0; next[0]=-1; while(j<m-1) { if(i==-1||b[i]==b[j]) { ++i; ++j; next[j]=i;} else i=next[i]; }}int Index_KMP(){ int i=0,j=0; while(i<n && j<m) { if(a[i]==b[j]||j==-1) { ++i; ++j;} else j=next[j]; } if(j>=m) return i-m+1; else return -1;}代码:Vie 阅读全文
posted @ 2011-04-08 09:51 聊聊IT那些事 阅读(320) 评论(0) 推荐(0)