字串匹配——KMP演算法

來源:互聯網
上載者:User

標籤:字串匹配   kmp演算法   java   

KMP演算法使用JAVA語言實現。

package BFKMPBM;public class BFMatching {public static void main(String[] args) {long startTime;long endTime;long durationTime;startTime = System.nanoTime();KMP();endTime = System.nanoTime();durationTime = endTime - startTime;System.out.println(durationTime);}//KMP演算法public static void KMP() {String KMP_S = "BBC ABCDAB ABCDABCDABDE";String KMP_T = "ABCDABD";int KMPSLength = KMP_S.length();//主串長度int KMPTLength = KMP_T.length();//子串長度for (int i = 0; i < KMPSLength;) {for (int j = 0; j < KMPTLength;) {if (KMP_S.charAt(i) != KMP_T.charAt(j)) {//49~55行主要是對模式串的觀察編寫if (j > 0 && j < 4) {i = i - 1;j = 0;} else if (j == 6) {i = i - 3;j = 0;}i++;} else if (KMP_S.charAt(i) == KMP_T.charAt(j)) {System.out.println(KMP_S.charAt(i) + "==" + KMP_T.charAt(j));if (j == 6) {System.out.println("匹配成功!");return;} i++;j++;} }}}}????


KMP演算法重點在於對模式串的觀察!建議在第28行設定斷點,單步調試,觀看代碼執行順序。在自己動手寫演算法代碼的時候才發現調試功能是如此的重要。

 

 

字串匹配——KMP演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.