Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write string matching KMP algorithm in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "string matching KMP algorithm in Java how to write", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "string matching KMP algorithm in Java how to write" it!

The violent string matching algorithm is easy to write, so take a look at its running logic:

Public class bf {public static int search (String txt,String pat) {int sLen = txt.length (); / / main string int pLen = pat.length (); / / pattern string length / / the number of times a match is needed (int iS0uti: a prefix is ba, a suffix is ba, and the maximum equivalent prefix length is 2.

For example, ababa-- > A prefix is aba, a suffix is aba, and the maximum equal prefix length is 3. Why not the prefix ababa and the suffix ababa, because the prefix does not contain the last character and the suffix does not contain the first character. Why not the prefix a suffix a, because although they are a pair of equal prefixes and suffixes, they are not the maximum equal prefix length

Set the I j pointer. I j is initially located at the beginning and index 1, which represents the prefix, suffix pointer and prefix suffix string, respectively. The prefix string always starts from 0, and the suffix string is always relative to the prefix string (the starting position cannot be determined), so whenever I j is mismatched, I will go back to the previous position, so how can I get back to the last position? This place is the most difficult to understand: ps [I] is the position where I should return. (NextI: if you are for the exam but still can't understand it, you don't have to take the time to understand it. Always remember that backtracking is I = next [I]. If you put a bunch of proof formulas here, I believe it must make you drowsy! ) then perform the next round of matching, and the next array will not be completed until j has finished.

Formal matching part: now let's discuss the relationship between the main string and the pattern string. The main string is the matched string, and the pattern string is the string of other strings to be matched (a bit of a mouthful). Usually the length of the pattern string is less than the main string. Use I j as the pointer of the main string pattern string, when the pattern string is relative and the main string is absolute, because if the main string does not match, you have to go back, and if the pattern string does not match, you have to re-match the previous part, so whenever I j mismatches, j will go back to the previous position (the same here) and make the next round of matching until j has finished, indicating that the match is successful and returning I-j. Because I-j corresponds to the starting position of the match. If the match does not match, I will come to the end and return-1

Thank you for reading, the above is the content of "how to write the string matching KMP algorithm in Java". After the study of this article, I believe you have a deeper understanding of how to write the string matching KMP algorithm in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report