Sunday algorithm for string matching

Source: Internet
Author: User

Sunday Algorithm core idea: Heuristic mobile search Step!

SUNDAY Algorithm Description:

The two most notable of the string lookup algorithms are the KMP algorithm (Knuth-morris-pratt) and the BM algorithm (Boyer-moore). This paper presents a Sunday lookup algorithm that is faster than the BM algorithm.

For example, to find "search" in "substring searching algorithm", the string is aligned with the left side of the text at first:

SUBSTRING searching algorithm
Search
^

The result finds a mismatch at the second character, so the string moves backwards. But how much does it move? This is where the various algorithms recount, the simplest way is to move a character position, the KMP is to use the information that has been matched to move, the BM algorithm is to do the reverse comparison, and according to the already matched parts to determine the amount of movement. The method to be introduced here is to look at the character immediately following the current substring (the ' I ' in).

Obviously, regardless of the number of moves, this character is sure to participate in the next comparison, that is, if the next match to, this character must be within the substring. So, you can move the substring so that the rightmost character in the substring aligns with it. Now that there is no ' I ' in the substring ' search ', it means that you can skip a large chunk, starting with the character after ' I ' to make the next comparison, such as:

SUBSTRING searching algorithm
Search
^

The result of the comparison, the first character does not match, and then look at the string after the character, is ' R ', it appears in the substring in the third-to-last, so the string forward three bits, so that two ' r ' alignment, as follows:

SUBSTRING searching algorithm
Search
^

Ha! This match was successful! Looking back at the whole process, we only moved two sub-strings to find the matching position, is not it very god ah?! It can be proved that with this algorithm, each step of the movement is larger than the BM algorithm, so certainly faster than the BM algorithm.

Therefore, for leetcode on the problem:

https://leetcode.com/problems/implement-strstr/

inch or if  is  not part of haystack.

The complete Python code is as follows:

classsolution (object):defstrStr (self, haystack, needle):""": Type Haystack:str:type needle:str:rtype:int refer:http://blog.csdn.net/kankan231/a rticle/details/22406823"""Char_pos=dict () forI, CHinchEnumerate (needle): Char_pos[ch]=I i=0 Len1=Len (haystack) Len2=len (needle) whileI <= len1-Len2:found=True forJ, CHinchEnumerate (needle):ifHAYSTACK[I+J]! =Ch:found=Falseif(I+LEN2) <len1:ifHAYSTACK[I+LEN2] not inchchar_pos:i+ = (len2+1)                        Else: I+ = (len2-char_pos[haystack[i+Len2]]) Else:                        return-1 Break            iffound:returnIreturn-1

Reference:http://blog.csdn.net/kankan231/article/details/22406823

Sunday algorithm for string matching

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.