kmp算法

 1 /**
 2      * @desc KMP算法
 3      * 基本介绍:
 4      * (1)暴力匹配算法
 5      *      1)如果当前字符匹配成功(即str1[i]=str2[i]),则i++,j++,继续匹配下一个字符
 6      *      2)如果失败,令i=i-(j-1),j=0,相当于每次匹配失败时,i回溯,j被转为0
 7      *      3)用暴力方法解决的话就会有大量的回溯,每次只移动一位,若是不匹配,移动到下一位接着判断,浪费大量时间。(不可行)
 8      *      4)暴力匹配实现
 9      * (2)KMP算法介绍
10      *      1)KMP是一个解决模式串在文本串是否出现过,如果出现过,最早出现的位置就经典算法。
11      *      2)Knuth-Morris-Pratt字符串查找法,简称KMP。
12      *      3)KMP算法就是利用之前判断过信息,通过一个next数组,保存模式串中前后最长公共序列的长度,每次回溯时,通过next数组找到,
13      *          前面匹配的位置,省去了大量的计算时间
14      *      4)参考资料:https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html
15      * @Author gavin
16      * @Date 2020/9/22
17      */
18     public static void main(String[] args) {
19             String str = "abcdeabdtyuiabcdeabdteehh";
20         String findStr = "deabdtegg";
21         System.out.println(KMP(str,findStr));
22     }
23 
24 public static int KMP(String s1,String s2){
25         if(s1.length()<s2.length()||s2.length()==0||null==s2 || null == s1) return -1;
26 
27         int[] temp = new int[s2.length()];
28         temp[0]=0;
29         int index = 0;
30         for (int i = 1; i < s2.length(); i++) {
31             if(s2.charAt(i)==s2.charAt(index)){
32                 index++;
33                 temp[i] = index;
34             }else{
35                 temp[i] = 0;
36                 index = 0;
37             }
38         }
39 
40         int j = 0;
41         for (int i = 0; i < s1.length();) {
42             if(s1.charAt(i)==s2.charAt(j)){
43                 if(s2.length()-1==j){
44                     return i+1-s2.length();
45                 }
46                 j++;
47                 i++;
48             }else{
49                 if(j==0){
50                     i++;
51                 }else{
52                     j = temp[j-1];
53                 }
54             }
55         }
56         return -1;
57     }

 

posted @ 2020-09-22 16:24  gavinwu_bk  阅读(155)  评论(0)    收藏  举报