标签:
KMP算法假定了解案件的原则,其实很easy。
关于根据自己的理解在这里。
KMP该算法由三个发明人的名称(Knuth、Morris、Pratt)的首字母组成,又称字符串查找算法。
个人认为能够理解为最小回溯算法,即匹配失效的时候,尽量少回溯。从而缩短时间复杂度。
KMP算法有两个关键的地方。1)求解next数组。2)利用next数组进行最小回溯。
当k=-1的时候。表示找不到这种最长前缀。
| j | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| p | a | b | a | a | b | c | a | b | a |
| next[j] | -1 | -1 | 0 | 0 | 1 | -1 | 0 | 1 | 2 |
下图表示了第一次不匹配。第二次匹配的过程,其他过程能够类推。当中 或 覆盖部分表示最长匹配串。 为待判定位置, 为已判定位置。
s ××××××××××××××××××××××××××××××××××××××××××××
p ××××××××××××××
在j处不失配时,前面的有部分匹配。这时须要利用next数组信息进行最小回溯。
s ××××××××××××××××××××××××××××××××××××××××××××
p ××××××××××××××
(这里 i 指向 s , j 指向 p。)
注意在 j = 0 的时候失配时。直接 i++ 就可以。
当 j > 0 的时候,须要利用next数组最快找到 p[ j ] == s[ i ] 的位置。
假设 j 移动到了0还找不到。则 i++,然后继续匹配。
这里我们能够发现仅仅有 j 回溯了,i没有回溯,可是因为普通版本号的 KMP 算法 j 须要不停地回溯直到找到合适的回溯位置,因此速度不是特别快。还能够继续优化。感兴趣的读者能够想想怎样事先求解好next数组从而不须要不停地回溯。
strStr返回的是首次匹配的地址。假设不能匹配则返回NULL。
class Solution {
public:
vector<int> getNext(char* &s){
vector<int> next(strlen(s), -1);
for(int i=1; i<strlen(s); i++){
int j = next[i-1]; /* 前一个字符的最长匹配长度 */
while(s[j+1] != s[i] && j>=0)
j = next[j];
if(s[j+1] == s[i])
next[i] = j+1;
// else 默觉得-1
}
return next;
}
char *strStr(char *haystack, char *needle) {
if(haystack==NULL || needle==NULL) return NULL;
if(strlen(haystack) < strlen(needle)) return NULL;
if(strlen(needle) == 0) return haystack;
vector<int> next = getNext(needle);
int i = 0;
int j = 0;
int haystackLen = strlen(haystack);
int needleLen = strlen(needle);
while(i<haystackLen && j<needleLen){
if(haystack[i] == needle[j] ) {
i++;
j++;
if(j == needleLen) return haystack + i - j;
}else{
if(j == 0) i++;
else j = next[j-1]+1; /* 该步骤能够优化 */
}
}
return NULL;
}
};
因为有人问有没有java版本号的,因为鄙人java比較挫。写java时部分还写成了scala的语法。不知道代码是否规范,有优化的地方还麻烦java方面的大神指点。
import java.util.*;
public class StrStrSolution {
private List<Integer> getNext(String p){
List<Integer> next = new ArrayList<Integer>();
next.add(-1);
for(int i=1; i<p.length(); i++){
int j = next.get(i-1);
while(p.charAt(j+1) != p.charAt(i) && j>=0)
j = next.get(j);
if(p.charAt(j+1) == p.charAt(i))
next.add( j + 1 );
else
next.add( -1 );
}
return next;
}
public String strStr(String haystack, String needle) {
if (haystack == null || needle == null) return null;
if (needle.length() == 0) return haystack;
if (needle.length() > haystack.length()) return null;
List<Integer> next = getNext(needle);
int i = 0;
int j = 0;
int haystackLen = haystack.length();
int needleLen = needle.length();
while(i < haystackLen && j < needleLen){
if(haystack.charAt(i) == needle.charAt(j) ) {
i++;
j++;
if(j == needleLen) return haystack.substring(i - j);
}else{
if(j==0) i++;
else j = next.get(j-1)+1;
}
}
return null;
}
public static void main(String[] args) {
String s = "babcabaabcacbac";
String p = "abaabcac";
StrStrSolution sol = new StrStrSolution();
System.out.println(sol.strStr(s,p));
}
}【数据结构&&等差数列】KMP简介和算法的实现(c++ && java)
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5043476.html