算法<初级> - 第七章 KMP/Manacher/BFPRT算法(完结)
2021-02-10 04:17
标签:har array 问题 没有 第k大 第七章 toc res ring KMP算法解决的问题:在str1字符串(长度n)中是否包含str2(长度m),返回-1或者首位置 next[]数组: 题目表述:给定一个字符串str1,只能往str1的后面添加字符变成str2。要求一:str2必须包含两个str1,两个str1可以有重合,但是不能以同一位置开头(不能完全重合)。要求二:str2尽量短。最终返回str2。 思路: 算法实现(java) 题目表述:给定两个二叉树T1和T2,返回T1的某个子树是否与T2的结构相等(就是T2为T1的某一子树) 思路: 算法实现(java) 求解最长的回文子串 RC与当前遍历i的关系: 算法实现(java) 题目表述:给定一个字符串str1,只能往str1的后面添加字符变成str2,要求str2整体都是回文串且最短。eg. str1=ABC12321,返回ABC12321CBA。 思路: 算法 - 第七章 KMP/Manacher/BFPRT算法(完结) 标签:har array 问题 没有 第k大 第七章 toc res ring 原文地址:https://www.cnblogs.com/ymjun/p/12746173.html算法 - 第七章 KMP/Manacher/BFPRT算法(完结)
KMP算法及其复杂度估计
[i]
存储的是模式串在i
位置前的子串中,其前缀和后缀最长的匹配长度
aaaa
,前缀a
=后缀a
,前缀aa
=后缀aa
,前缀aaa
=后缀aaa
,规定前后缀为子串真子集,所以最长前后缀匹配长度为3。 public static int getIndexOf(String s, String m) { //kmp算法
if (s == null || m == null || m.length() 0) {
cn = next[cn];
} else {
next[pos++] = 0;
}
}
return next;
}
public static void main(String[] args) {
String str = "abcabcababaccc";
String match = "ababa";
System.out.println(getIndexOf(str, match));
}
KMP算法扩展题目一 - ShortestHaveTwice
public static String answer(String str) {
if (str == null || str.length() == 0) {
return "";
}
char[] chas = str.toCharArray();
if (chas.length == 1) {
return str + str;
}
if (chas.length == 2) {
return chas[0] == chas[1] ? (str + String.valueOf(chas[0])) : (str + str);
}
int endNext = endNextLength(chas);
return str + str.substring(endNext);
}
public static int endNextLength(char[] chas) { // 就是求解next数组,只不过直接返回next[]位置上的值即可。
int[] next = new int[chas.length + 1];
next[0] = -1;
next[1] = 0;
int pos = 2;
int cn = 0;
while (pos 0) {
cn = next[cn];
} else {
next[pos++] = 0;
}
}
return next[next.length - 1];
}
public static void main(String[] args) {
String test1 = "a";
System.out.println(answer(test1));
String test2 = "aa";
System.out.println(answer(test2));
String test3 = "ab";
System.out.println(answer(test3));
String test4 = "abcdabcd";
System.out.println(answer(test4));
String test5 = "abracadabra";
System.out.println(answer(test5));
}
KMP算法扩展题目二 - T1SubtreeEqualsT2
#
特殊字符表示,每走一个结点输出再加上_
(一个表示结束一个表示空)。eg. 1_2_#_#_3_#_#_
,表示1->2左3右的一棵二叉树。(使用前+中序这种还原树方法是没有办法还原结点数据全都相同的情况)
12_#_#_
和2_#_#_
) public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static boolean isSubtree(Node t1, Node t2) {
String t1Str = serialByPre(t1);
String t2Str = serialByPre(t2);
return getIndexOf(t1Str, t2Str) != -1;
}
public static String serialByPre(Node head) {
if (head == null) {
return "#!";
}
String res = head.value + "!";
res += serialByPre(head.left);
res += serialByPre(head.right);
return res;
}
// KMP
public static int getIndexOf(String s, String m) {
if (s == null || m == null || m.length() 0) {
cn = nextArr[cn];
} else {
nextArr[pos++] = 0;
}
}
return nextArr;
}
public static void main(String[] args) {
Node t1 = new Node(1);
t1.left = new Node(2);
t1.right = new Node(3);
t1.left.left = new Node(4);
t1.left.right = new Node(5);
t1.right.left = new Node(6);
t1.right.right = new Node(7);
t1.left.left.right = new Node(8);
t1.left.right.left = new Node(9);
Node t2 = new Node(2);
t2.left = new Node(4);
t2.left.right = new Node(8);
t2.right = new Node(5);
t2.right.left = new Node(9);
System.out.println(isSubtree(t1, t2));
}
Manacher算法及其复杂度估计
public static char[] manacherString(String str) {
char[] charArr = str.toCharArray();
char[] res = new char[str.length() * 2 + 1];
int index = 0;
for (int i = 0; i != res.length; i++) {
res[i] = (i & 1) == 0 ? ‘#‘ : charArr[index++];
}
return res;
}
public static int maxLcpsLength(String str) {
if (str == null || str.length() == 0) {
return 0;
}
char[] charArr = manacherString(str);
int[] pArr = new int[charArr.length];
int index = -1;
int pR = -1;
int max = Integer.MIN_VALUE;
for (int i = 0; i != charArr.length; i++) {
pArr[i] = pR > i ? Math.min(pArr[2 * index - i], pR - i) : 1;
while (i + pArr[i] -1) {
if (charArr[i + pArr[i]] == charArr[i - pArr[i]])
pArr[i]++;
else {
break;
}
}
if (i + pArr[i] > pR) {
pR = i + pArr[i];
index = i;
}
max = Math.max(max, pArr[i]);
}
return max - 1;
}
public static void main(String[] args) {
String str1 = "abc1234321ab";
System.out.println(maxLcpsLength(str1));
}
Manacher算法扩展题目:ShortestEnd
public static char[] manacherString(String str) {
char[] charArr = str.toCharArray();
char[] res = new char[str.length() * 2 + 1];
int index = 0;
for (int i = 0; i != res.length; i++) {
res[i] = (i & 1) == 0 ? ‘#‘ : charArr[index++];
}
return res;
}
public static String shortestEnd(String str) {
if (str == null || str.length() == 0) {
return null;
}
char[] charArr = manacherString(str);
int[] pArr = new int[charArr.length];
int index = -1;
int pR = -1;
int maxContainsEnd = -1;
for (int i = 0; i != charArr.length; i++) {
pArr[i] = pR > i ? Math.min(pArr[2 * index - i], pR - i) : 1;
while (i + pArr[i] -1) {
if (charArr[i + pArr[i]] == charArr[i - pArr[i]])
pArr[i]++;
else {
break;
}
}
if (i + pArr[i] > pR) {
pR = i + pArr[i];
index = i;
}
if (pR == charArr.length) {
maxContainsEnd = pArr[i];
break;
}
}
char[] res = new char[str.length() - maxContainsEnd + 1];
for (int i = 0; i
BFPRT算法及其复杂度估计
// O(N*logK)
public static int[] getMinKNumsByHeap(int[] arr, int k) {
if (k arr.length) {
return arr;
}
int[] kHeap = new int[k];
for (int i = 0; i != k; i++) {
heapInsert(kHeap, arr[i], i);
}
for (int i = k; i != arr.length; i++) {
if (arr[i] arr[index]) {
largest = left;
}
if (right arr[largest]) {
largest = right;
}
if (largest != index) {
swap(arr, largest, index);
} else {
break;
}
index = largest;
left = index * 2 + 1;
right = index * 2 + 2;
}
}
// O(N)
public static int[] getMinKNumsByBFPRT(int[] arr, int k) {
if (k arr.length) {
return arr;
}
int minKth = getMinKthByBFPRT(arr, k);
int[] res = new int[k];
int index = 0;
for (int i = 0; i != arr.length; i++) {
if (arr[i] = pivotRange[0] && i pivotValue) {
swap(arr, cur, --big);
} else {
cur++;
}
}
int[] range = new int[2];
range[0] = small + 1;
range[1] = big - 1;
return range;
}
public static int getMedian(int[] arr, int begin, int end) {
insertionSort(arr, begin, end);
int sum = end + begin;
int mid = (sum / 2) + (sum % 2);
return arr[mid];
}
public static void insertionSort(int[] arr, int begin, int end) {
for (int i = begin + 1; i != end + 1; i++) {
for (int j = i; j != begin; j--) {
if (arr[j - 1] > arr[j]) {
swap(arr, j - 1, j);
} else {
break;
}
}
}
}
public static void swap(int[] arr, int index1, int index2) {
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
public static void printArray(int[] arr) {
for (int i = 0; i != arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = { 6, 9, 1, 3, 1, 2, 2, 5, 6, 1, 3, 5, 9, 7, 2, 5, 6, 1, 9 };
// sorted : { 1, 1, 1, 1, 2, 2, 2, 3, 3, 5, 5, 5, 6, 6, 6, 7, 9, 9, 9 }
printArray(getMinKNumsByHeap(arr, 10));
printArray(getMinKNumsByBFPRT(arr, 10));
}
上一篇:python学习之基础
下一篇:数组(Array)
文章标题:算法<初级> - 第七章 KMP/Manacher/BFPRT算法(完结)
文章链接:http://soscw.com/index.php/essay/53402.html