KMP算法及其扩展
假设字符串str长度为N,字符串match长度为M,M <= N,想确定str中是否有某个子串是等于match的。时间复杂度O(N)
指标,i 位置前的字符串的,前缀与后缀相同的最大长度,不能等于整体
建立这样一个指标数组
a | a | b | a | a | b | c |
-1 | 0 | 1 | 0 | 1 | 2 | 3 |
-1和0人为规定
public static int getIndexOf(String s, String m) {
if (s == null || m == null || m.length() < 1 || s.length() < m.length()) {
return -1;
}
char[] str = s.toCharArray();
char[] match = m.toCharArray();
int x = 0; // str 中当前对比到的位置
int y = 0; // match 中当前对比到的位置
int[] next = getNextArray(match); // nexts[i] match中i之前的字符串match[0..i-1]
while (x < str.length && y < match.length) {
if (str[x] == match[y]) {
x++;
y++;
} else if (next[y] == -1) { // y == 0 来到了match的开头,换个位置开始匹配
x++;
} else {
// 来到最长前缀的下一个位置
y = next[y];
}
}
// 返回首个匹配到的开头位置
return y == match.length ? x - y : -1;
}
public static int[] getNextArray(char[] ms) {
if (ms.length == 1) {
return new int[] { -1 };
}
int[] next = new int[ms.length];
next[0] = -1;
next[1] = 0;
int i = 2;
// cn代表,cn位置的字符,是当前和i-1位置比较的字符
int cn = 0;
while (i < next.length) {
if (ms[i - 1] == ms[cn]) {
next[i++] = ++cn;
} else if (cn > 0) {
cn = next[cn];
} else {
next[i++] = 0;
}
}
return next;
}
为什么 y = next[y]
为什么 cn = next[cn]
例1:有str1,判断str2是不是str1的旋转词
生成 String s = str1 + str1
判断str2是不是s的子串,是就是旋转词,不是就不是旋转词
例2:给定两棵二叉树的头节点head1和head2,想知道head1中是否有某个子树的结构和head2完全一样?
先序序列化两颗二叉树为两个列表,认为存的都是字符,然后应用KMP算法看head2是不是head1的子串,是就有完全一样的子树,不是就没有
直接序列化为字符串是不行的,例如
1020 和 20 序列化成字符串的话就有歧义 如果是严格用特殊字符分割每个值,序列化成字符串也可以
/ /
3 3
public static boolean containsTree2(Node big, Node small) {
if (small == null) {
return true;
}
if (big == null) {
return false;
}
ArrayList<String> b = preSerial(big);
ArrayList<String> s = preSerial(small);
String[] str = new String[b.size()];
for (int i = 0; i < str.length; i++) {
str[i] = b.get(i);
}
String[] match = new String[s.size()];
for (int i = 0; i < match.length; i++) {
match[i] = s.get(i);
}
return getIndexOf(str, match) != -1;
}
public static ArrayList<String> preSerial(Node head) {
ArrayList<String> ans = new ArrayList<>();
pres(head, ans);
return ans;
}
public static void pres(Node head, ArrayList<String> ans) {
if (head == null) {
ans.add(null);
} else {
ans.add(String.valueOf(head.value));
pres(head.left, ans);
pres(head.right, ans);
}
}
public static int getIndexOf(String[] str1, String[] str2) {
if (str1 == null || str2 == null || str1.length < 1 || str1.length < str2.length) {
return -1;
}
int x = 0;
int y = 0;
int[] next = getNextArray(str2);
while (x < str1.length && y < str2.length) {
if (isEqual(str1[x], str2[y])) {
x++;
y++;
} else if (next[y] == -1) {
x++;
} else {
y = next[y];
}
}
return y == str2.length ? x - y : -1;
}
public static int[] getNextArray(String[] ms) {
if (ms.length == 1) {
return new int[] { -1 };
}
int[] next = new int[ms.length];
next[0] = -1;
next[1] = 0;
int i = 2;
int cn = 0;
while (i < next.length) {
if (isEqual(ms[i - 1], ms[cn])) {
next[i++] = ++cn;
} else if (cn > 0) {
cn = next[cn];
} else {
next[i++] = 0;
}
}
return next;
}
public static boolean isEqual(String a, String b) {
if (a == null && b == null) {
return true;
} else {
if (a == null || b == null) {
return false;
} else {
return a.equals(b);
}
}
}