File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 58
58
59
59
----
60
60
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
+
61
91
本书内容几乎完全来源于网络。
62
92
63
93
开源项目地址:[ https://github.com/hustcc/JS-Sorting-Algorithm ] ( https://github.com/hustcc/JS-Sorting-Algorithm ) ,整理人 [ hustcc] ( https://github.com/hustcc ) 。
You can’t perform that action at this time.
0 commit comments