KMP algorithm Java version __ algorithm

Source: Internet
Author: User

1 Principle

Refer to the section 5.7 of the 5th chapter of the "Liar data Structure": KMP Pattern matching algorithm


2 Java Code

Kmp.java

Package Leaning.string.KMP; public class KMP {public static int indexkmp (string source, String Target,int pos) {/* I is used for subscript values at the current position of the main string source, if the POS is not 
		
		1 from POS position start to match/int i = pos;
		
		/* j for substring Target in the current position subscript/Int J = 1;  
		
		/* Define a set of next array */int[] next = new Int[intarraylength.length];
		
		/* Target is parsed to get next array */next = Nextutil.getnext (target);
		Converts a source,target string into a character array char[] Sourcechar = Kmpstringutil.stringtochar (source);
		
		char[] Targetchar = Kmpstringutil.stringtochar (target); If I is less than the source length and target is less than the target length, execute the loop while (I <=source.length () && J <= Target.length ()) {if j==0 | | s
				Ourcechar[i] = = Targetchar[j]) {++i;
			++j;
			}else{j = Next[j];
		} if (J > Target.length ()) {return i-target.length ();	
		}else{return 0;
		} public static void Main (string args[]) {string source = ' ASDKFJSLKFJWEJDKLFJEW8ODKLFJSDLKFJSLKDJ ';
		String target = "W8odkl";
		int pos = 0; int p = kmp.indexkmP (source, target, POS);
	System.out.println ("in position" +p + "exact Match");
 }
}

Nextutil.java

Package Leaning.string.KMP;

public class Nextutil {
	
	/**
	 * * 
	 gets next array 
	 *
	/public static int[] GetNext (String targetstr) {
       if (targetstr==null) return null;
       char[] targetmodify = Kmpstringutil.stringtochar (TARGETSTR); Change Targetstr string to character array
       int length = Targetstr.length ();
       Get next array
       int i,j;
       i = 1;
       j = 0;
       int next[] = new Int[intarraylength.length];
       while (I < length) {
    	   if (j = = 0 | | targetmodify[i] = = Targetmodify[j]) {//targetmodify[i] denotes suffix single character, targetmodify[j] table A single character ++i that shows a prefix 
    		   ;
    		   ++j;
    		   Next[i] = j;
    	   } else{
    		   j = next[j];//If the characters are different, J makes a backtracking} and return
       
	   next;  

	public static void Main (string args[]) {
		string targetstr = "Ababaaaba";
		int next[] = Nextutil.getnext (TARGETSTR);
		for (int i = 0; i < next.length i++) {
			System.out.print (Next[i] + "");}
		}




Intarraylength.java

Package Leaning.string.KMP;

Public  class Intarraylength {
   public  static int length = 255;
}

Kmpstringutil.java

Package Leaning.string.KMP;

The public class Kmpstringutil {public
   
	static char[] Stringtochar (String source) {
		 
	       /**
	        * is transforming source. Make its first digit a placeholder
	        *    Source =    a b c d e F g h
	        * targetmodify = ' A b c d e F g h 
	        * 
	        * * * * * * *
	       char[ ] Targetmodify = new Char[source.length () +1];
	       int length = Source.length (); 
	       for (int i = 0; i < source.length (); i++) {
	    	   targetmodify[i+1] = Source.charat (i);
	       }
		Return targetmodify
	} 
	
}

3 Running Results

Run Kmp.java get the following results



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.