Skip to content

Commit ecc6c1d

Browse files
authored
Update README.md
1 parent 75a861b commit ecc6c1d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@
5858

5959
----
6060

61+
KMP算法
62+
public class KMP {
63+
private int[][] dp;
64+
private String pat;
65+
66+
public KMP(String pat) {
67+
this.pat = pat;
68+
int M = pat.length();
69+
// dp[状态][字符] = 下个状态
70+
dp = new int[M][256];
71+
// base case
72+
dp[0][pat.charAt(0)] = 1;
73+
// 影子状态 X 初始为 0
74+
int X = 0;
75+
// 当前状态 j 从 1 开始
76+
for (int j = 1; j < M; j++) {
77+
for (int c = 0; c < 256; c++) {
78+
if (pat.charAt(j) == c)
79+
dp[j][c] = j + 1;
80+
else
81+
dp[j][c] = dp[X][c];
82+
}
83+
// 更新影子状态
84+
X = dp[X][pat.charAt(j)];
85+
}
86+
}
87+
88+
public int search(String txt) {...}
89+
}
90+
6191
本书内容几乎完全来源于网络。
6292

6393
开源项目地址:[https://github.com/hustcc/JS-Sorting-Algorithm](https://github.com/hustcc/JS-Sorting-Algorithm),整理人 [hustcc](https://github.com/hustcc)

0 commit comments

Comments
 (0)