/**
* KMP算法解决字符匹配问题
*/
public class KMP {
public static void main(String[] args) {
String str1="ABABACEF";
String str2="BAC";
//System.out.println(foolWay2(str1,str2));
// System.out.println(kmpSearch(str1,str2,kmpNext(str2)));
//ABCDABD
System.out.println(Arrays.toString(kmpNext("ABAABA")));
}
//暴力法
static int foolWay(String str1,String str2){
int str1Length=str1.length();
int str2Length=str2.length();
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
//定义两个指针,str1P是str1的指针,str2P是str2的指针
int str1P=0;
int str2P=0;
while (str1P<str1Length && str2P<str2Length){
if(chars1[str1P] == chars2[str2P]){
str1P++;
str2P++;
}else{
//如果
使用KMP算法进行字符串查找
最新推荐文章于 2022-01-01 11:13:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)