Java資料結構之字串模式比對演算法---KMP演算法2

來源:互聯網
上載者:User

標籤:結構   匹配   模式   div   尾碼   src   位置   ges   stat   

 

直接接上篇上代碼:

 1 //KMP演算法 2 public class KMP { 3  4     // 擷取next數組的方法,根據給定的字串求 5     public static int[] getNext(String sub) { 6  7         int j = 1, k = 0; 8         int[] next = new int[sub.length()]; 9         next[0] = -1; // 這個是規定10         next[1] = 0; // 這個也是規定11         //12         while (j < sub.length() - 1) {13             if (sub.charAt(j) == sub.charAt(k)) {14                 next[j + 1] = k + 1;15                 j++;16                 k++;17             } else if (k == 0) {18                 next[j + 1] = 0;19                 j++;20             } else {21                 k = next[k];22             }23 24         }25         return next;26     }27 28     // 根據給定的主串和子串,採用KMP演算法來擷取模式比對29     public static int kmp(String src, String sub) {30 31         // 首先產生模式串sub的next[j]32         int[] next = getNext(sub);33         int i = 0, j = 0, index = -1;34         while (i < src.length() && j < sub.length()) {35             if (src.charAt(i) == sub.charAt(j)) {36                 i++;37                 j++;38             } else if (j == 0) {39                 i++;40             } else {41                 j = next[j];42             }43         }44 45         // 得到開始匹配的位置索引46         if (j == sub.length()) {47             index = i - sub.length();48         }49         return index;50     }51 }

//

 1 //KMP演算法的測試 2 public class Test { 3     /** 4      * @param args 5      */ 6     public static void main(String[] args) { 7         String src = "aaaaaaab"; 8         String sub = "ABCDABDABD"; 9         int[] next = KMP.getNext(sub);10         //int[] next = lengthKMP(sub.toCharArray());11         for (int i : next) {12             System.out.print(i + "  ");// -1 0 1 2 313         }14 15         // System.out.println();16         // System.out.println(KMP.kmp(src, sub));17     }18 19     //補充上一篇中的對於首碼尾碼的討論的擷取部分匹配數組的演算法20     public static int[] lengthKMP(char[] mchar) {21         int[] fixNum = new int[mchar.length];22         for (int i = 1, j = 0; i < mchar.length; i++) {23             if (mchar[j] == mchar[i]) {24                 fixNum[i] = j + 1;25                 j++;26             } else if (j > 0) {27                 j = 0;28                 i -= j;29             }30         }31         // return [0, 0, 0, 0, 1, 2, 0, 1, 2, 0]ABCDABDABD32         return fixNum;33     }34 }

 

Java資料結構之字串模式比對演算法---KMP演算法2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.