Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
非本题代码!kmp的next求法还是有点讲究的void nextvalue() { int plen = P.length(); int j = 0; int k = -1; next[0] = -1; while(j < plen - 1) { if(k == -1 || P[j] == P[k]) { j++; k++; if(P[j] == P[k]) next[j] = next[k];//和下面注释都是关键 else next[j] = k;//还有这里 } else k = next[k]; } } int kmp() { int i,j; i = j = 0; int plen = P.length(); int slen = S.length(); while(i < slen && j < plen) { if(j == -1 || S[i] == P[j]) { i++; j++; } else j = next[j]; } if(j == plen) return i - j; else return -1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator