forked from rbmonster/learning-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
563 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.learning.algorithm; | ||
|
||
/** | ||
* KMP算法本质上是模拟指针的回退 | ||
* 适用于重复性很高的文本 中查找重复性很高的"模式"(即字符串) | ||
*/ | ||
public class KMP { | ||
|
||
private String pat; | ||
|
||
private int[][] dfa; | ||
|
||
public KMP(String pat) { | ||
this.pat = pat; | ||
int M = pat.length(); | ||
// 字母表的大小R | ||
int R = 256; | ||
dfa = new int[R][M]; | ||
dfa[pat.charAt(0)][0] = 1; | ||
// j = 1 忽略首字母 表示模式需要右移动一位 | ||
for (int j = 1, X = 0; j < M; j++) { | ||
for (int c = 0; c < R; c++) { | ||
// 复制模式失败情况下的值 | ||
dfa[c][j] = dfa[c][X]; | ||
} | ||
// 设置匹配成功情况下的值, dfa[pat.charAt(j)][j] 总是 j+1 表示匹配上往前进一位 | ||
dfa[pat.charAt(j)][j] = j+1; | ||
// 更新重启状态 | ||
X = dfa[pat.charAt(j)][X]; | ||
} | ||
} | ||
|
||
|
||
public int search(String txt) { | ||
int i, j , N = txt.length(), M = pat.length(); | ||
for (i = 0, j = 0; i < N && j<M ; i++) { | ||
j = dfa[txt.charAt(i)][j]; | ||
} | ||
if (j == M) { | ||
return i-M; // 找到匹配 | ||
} | ||
else { | ||
return N; // 未找到匹配 | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
KMP kmp = new KMP("sadfjweasdfasd"); | ||
kmp.search("qwejriasdnfzxc,mvnz,xmcnvasdf"); | ||
|
||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
src/main/java/com/learning/algorithm/basic/dynamic/KMP.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.learning.algorithm.trie; | ||
|
||
/** | ||
* 三向单词查找树 | ||
* | ||
* | ||
* @param <T> | ||
*/ | ||
public class TST<T> { | ||
|
||
private Node<T> root; // 单词查找树 根节点 | ||
|
||
private static class Node<T> { | ||
private T val; | ||
|
||
char c; | ||
|
||
private Node<T> mid, left, right; | ||
} | ||
|
||
public T get(String str) { | ||
Node<T> x = get(root, str, 0); | ||
if (x == null) { | ||
return null; | ||
} | ||
return x.val; | ||
} | ||
|
||
private Node<T> get(Node<T> node, String str, int d) { | ||
if (node == null) { | ||
return null; | ||
} | ||
char ch = str.charAt(d); | ||
if (ch < node.c) { | ||
return get(node.right, str, d); | ||
} else if (ch < node.c) { | ||
return get(node.left, str, d); | ||
} else { | ||
if (d < str.length() - 1) { | ||
return get(node.mid, str, d + 1); | ||
} else { | ||
return node; | ||
} | ||
} | ||
} | ||
|
||
public void put(String str, T value) { | ||
this.root = put(root, str, 0, value); | ||
} | ||
|
||
private Node<T> put(Node<T> node, String str, int d, T value) { | ||
char ch = str.charAt(d); | ||
if (node == null) { | ||
node = new Node<T>(); | ||
node.c = ch; | ||
} | ||
if (ch < node.c) { | ||
node.left = put(node.left, str, d, value); | ||
} else if (ch > node.c) { | ||
node.right = put(node.right, str, d, value); | ||
} else { | ||
if (d < str.length() - 1) { | ||
node.mid = put(node.mid, str, d + 1, value); | ||
} else { | ||
node.val = value; | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.