Simple and KMP pattern matching algorithm (Java)

Source: Internet
Author: User

Simple and KMP pattern matching algorithm (Java)
Simple pattern matching algorithm

Public class Test {// simple mode matching algorithm public int Index (String s, String t, int pos) {int I = pos; // compare the first position of the Main string to int j = 0; // the first position in the mode string while (I
  
   
= T. length () {return i-t.length ();} else {return-1 ;}} public static void main (String [] args) {Test test = new Test (); System. out. println (test. index (goodgoogle, google, 0); System. out. println (test. index (goodgoogle, google, 2); System. out. println (test. index (googlegoogle, google, 0); System. out. println (test. index (googlegoogle, google, 2 ));}}
  

Running result:

4406
KMP pattern matching algorithm

Public class Test {public static void main (String [] args) {Test test = new Test (); test. getNext (abcabx); test. getNext (abcdex); System. out. println (test. indexKMP (goodgoogle, google, 0 ));} //////// KMP mode matching algorithm // public int [] getNext (String T) {int [] next = new int [T. length ()]; int I = 0; int j =-1; next [I] = j; while (I
  
   
= T. length () {return i-T.length () ;}else {return 0 ;}}}
  

Running result:

-100012-100000-1000104

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.