diff --git a/README.md b/README.md
index fd2b010..e44834a 100644
--- a/README.md
+++ b/README.md
@@ -9,4 +9,4 @@
## Thanks to the following organizations for providing open source licenses
-[](https://jb.gg/OpenSource)
\ No newline at end of file
+[](https://jb.gg/OpenSource)
diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/match/BruteForce.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/match/BruteForce.java
index affa1ff..6b866e0 100644
--- a/budd-common/src/main/java/io/github/ehlxr/algorithm/match/BruteForce.java
+++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/match/BruteForce.java
@@ -39,20 +39,25 @@ public class BruteForce {
}
public static int bf(String s, String p) {
- int sl = s.length();
- int pl = p.length();
+ int m = s.length();
+ int n = p.length();
- for (int i = 0; i <= sl - pl; i++) {
+ for (int i = 0; i <= m - n; i++) {
int j = 0;
- for (; j < pl; j++) {
- if (s.charAt(i) == p.charAt(j)) {
- i++;
- } else {
+ for (; j < n; j++) {
+ // if (s.charAt(i) == p.charAt(j)) {
+ // i++;
+ // } else {
+ // break;
+ // }
+ // 如果主串与模式串不匹配,则主串向右移动一个字符,模式串从头开始匹配
+ if (s.charAt(i + j) != p.charAt(j)) {
break;
}
}
- if (j == pl) {
- return i - pl;
+ if (j == n) {
+ // return i - n;
+ return i;
}
}
diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/match/Kmp.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/match/Kmp.java
index b5f0d85..cbfacc2 100644
--- a/budd-common/src/main/java/io/github/ehlxr/algorithm/match/Kmp.java
+++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/match/Kmp.java
@@ -52,15 +52,33 @@ public class Kmp {
for (int i = 0; i <= m - n; i++) {
int j = 0;
- while (j < n) {
- if (scs[i] == pcs[j]) {
- i++;
- j++;
- } else {
- // 当模式串与主串不匹配时,如果**不匹配字符**对应模式串下标大于 j > 0 (非首个模式串字符),
- // 并且此字符前一个字符对应字符串部分匹配表中的值 next[j - 1] 也大于 0,
- // j - next[j - 1] 即模式串为后移的位数,等价于 j 置为 next[j - 1]
+ // while (j < n) {
+ // if (scs[i] == pcs[j]) {
+ // i++;
+ // j++;
+ // } else {
+ // // 当模式串与主串不匹配时,如果**不匹配字符**对应模式串下标大于 j > 0 (非首个模式串字符),
+ // // 并且此字符前一个字符对应字符串部分匹配表中的值 next[j - 1] 也大于 0,
+ // // j - next[j - 1] 即模式串为后移的位数,等价于 j 置为 next[j - 1]
+ // if (j > 0 && next[j - 1] > 0) {
+ // j = next[j - 1];
+ // } else {
+ // break;
+ // }
+ // }
+ // }
+ // if (j == n) {
+ // return i - n;
+ // }
+
+ for (; j < n; j++) {
+ if (scs[i + j] != pcs[j]) {
+ // 暴力匹配算法当模式串和主串不匹配时,主串匹配下标 +1,模式串匹配下标置为 0,
+ // KMP 算法优化点在于将模式串下标置为不匹配字符前一个字符对应 next 数组的值
if (j > 0 && next[j - 1] > 0) {
+ // 当模式串与主串不匹配时,如果**不匹配字符**对应模式串下标大于 j > 0 (非首个模式串字符),
+ // 并且此字符前一个字符对应字符串部分匹配表中的值 next[j - 1] 也大于 0,
+ // j - next[j - 1] 即模式串为后移的位数,等价于 j 置为 next[j - 1]
j = next[j - 1];
} else {
break;
@@ -68,7 +86,7 @@ public class Kmp {
}
}
if (j == n) {
- return i - n;
+ return i;
}
}
diff --git a/jetbrains.svg b/jetbrains.svg
new file mode 100644
index 0000000..75d4d21
--- /dev/null
+++ b/jetbrains.svg
@@ -0,0 +1,66 @@
+
+
+