Java data Structure-string and its application-KMP pattern matching algorithm

Source: Internet
Author: User

A string (string) is a finite sequence of 0 or more woo characters, also called a string.

Explanation of the definition:
?? The number of characters in a string n is called the length of a string, and the definition of "finite" means that the length n is a finite number.
?? A string of 0 characters is called an empty string (a null string), it is zero in length, can be represented directly by two double quotation marks, or it can be represented by the Greek φ letter.
?? The so-called sequence, which illustrates the relationship between the adjacent characters of a string and the predecessor and successor.

Here are some conceptual things about the string:

?? A string of spaces that contains only spaces. Notice the difference between it and the empty string, the space string is the content has a length, and can be more than one space.

?? A sub-sequence of substrings and a main string, any number of successive characters in a string, is called a substring of that string, and accordingly, a string containing substrings is called the main string. The position of the substring in the main string is the ordinal number of the first character of the substring in the main string.

?? String comparisons are made by encoding the characters between the strings, while the character encoding refers to the sequence number of the character in the corresponding character set. Compare two strings for equality, which must be the length of their strings and the characters of each corresponding position are equal.

Comparison of strings to linear tables :
?? The logical structure of the two is similar, the difference is that the string is for the character set (that is, the elements in the string are characters).
?? Linear tables are more concerned with the operation of a single element, such as finding an element, inserting or deleting an element, but more of the string is finding the location of the substring, getting the specified position substring, replacing the substring, and so on.
?? The sequential storage structure of a string stores the sequence of characters in a string using a contiguous set of storage units. Assigns a fixed-length store to each defined string variable, according to the predefined size. It is usually defined by a fixed-length array.

Attention:
?? String connection operation, you need to pay attention to the string length problem, you may encounter the problem is that the two string connection will not overflow, so the need for truncation operation.

To find the position of the substring in the main string, proceed as follows:
? 1. Set I for the current position subscript value in the main string S1, J for the current position subscript value in the sub-string sub.
? 2. First we compare s1[1] and sub[1], if the same, maybe the substring begins.
? 3. If not equal, then the substring is still starting with sub[1], while the main string S1 is compared with s1[2].
? 4. If the substring length or above match, then the substring is found, at this time J must be greater than the substring length sub[0].

KMP Pattern matching algorithm:
?? This thing is somewhat difficult to understand, below is my study time some experience, hoped to be helpful to everybody.

KMP pattern matching algorithm, do not understand the words suggested to fish C watch Video: http://study.163.com/course/courseMain.htm?courseId=468002, speak very good, very detailed;
Then for the pre-and-post video inside is not very detailed and clear, not understand the words can go to see Nanyi this article http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80% 93pratt_algorithm.html

There are also two KMP recommendations:
A thorough understanding of the KMP (August 22, 2014 edition)-structure of the method of the algorithm-blog channel-csdn.net
http://blog.csdn.net/v_july_v/article/details/7041827

KMP Algorithm Learning Preliminary | Sea Rainbow not old pavilion--looking at the wolf in the north
Http://haihongblog.com/archives/911.html

Here is my Java implementation version:

 Packagecom.phn.string;ImportJava.util.Arrays;/** * @author pan Hainan * @Email [Email protected] * @TODO pattern matching algorithm * @date July 26, 2015 */< /c0> Public  class fokmp {    Private Static int[]GetNext(String Str) {int[] Next =New int[Str.length ()];intI, J; i =0; j =-1; next[0] = -1; while(I < str.length ()-1) {if(j = =-1||                Str.charat (i) = = Str.charat (j)) {i++; j + +;if(Str.charat (i)! = Str.charat (j))                {Next[i] = j; }Else{Next[i] = next[j]; }            }Else{j = next[j]; }        }returnNext } Public Static int INDEXKMP(String S, String T) {inti =0;intj =0;int[] Next = GetNext (T); System.out.println (Arrays.tostring (next)); while(I <= s.length ()-1&& J <= T.length ()-1) {if(j = =-1||                S.charat (i) = = T.charat (j)) {i++;            j + +; }Else{j = next[j]; }        }if(J >= T.length ())returnI-t.length ();return-1; }}

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.

Java data Structure-string and its application-KMP pattern matching algorithm

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.