Step-by-step write algorithm (the string lookup medium)

Source: Internet
Author: User

Text: Step by step Write algorithm (the string lookup medium)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Yesterday we wrote a simple character lookup function. Although relatively simple, but also can be used. However, after careful analysis, there is room for improvement in such a simple function. Where do you improve it? Everybody can look down slowly.

The following code is the pre-optimization code, which is now pasted again, which makes it easier to analyze:

char* strstr (const char* STR, char* data) {int Index;int len;if (NULL = = STR | | NULL = = str) return Null;len = strlen (data), while (*str && (int.) strlen (str) >= len) {for (index = 0; index < len ; Index + +) {if (Str[index]! = Data[index]) break;} if (index = = len) return (char*) str;str++;} return NULL;}
Do not know friends found no, the original while condition has a very time-consuming operation. That is, each time STR moves, it is necessary to determine the length of Str. If STR is longer than the length of data, the time to calculate str length is considerable.

int check_length_of_str (const char* str, int len) {int index;for (index = 0; index < len; index + +) {if (' = = Str[index] ) return 0;} return 1;} char* strstr (const char* STR, char* data) {int Index;int len;if (NULL = = STR | | NULL = = str) return Null;len = strlen (data), while (*str && check_length_of_str (str, len)) {for (index = 0; Index < Len Index + +) {if (Str[index]! = Data[index]) break;} if (index = = len) return (char*) str;str++;} return NULL;}
The above code solves the problem of length judgement very well, so that the length of each comparison is very short, so long as the size of Len is judged by the length of the character. However, we are not very satisfied, if the two do not compare better. So, is there a possibility? We found that if STR was not successful each time, it would increment itself. So we just have to judge if this one is ' no '? So, our code can also be written in the following form.

char* strstr (const char* STR, char* data) {int Index;int len;if (NULL = = STR | | NULL = = str) return Null;len = strlen (data), if (int) strlen (str) < Len) return Null;while (*STR) {for (index = 0; Index < Len Index + +) {if (Str[index]! = Data[index]) break;} if (index = = len) return (char*) str;if (' + = = Str[len]) break;str++;} return NULL;}
Unlike the first optimization above, we will judge the difference in length before entering the while, but after the first judgment, we will no longer have to judge, because the next we can only determine whether the nth element is ' n-1 ', the original element we have already judged, must be a legitimate element. Why is it? Everyone can think about it.


(ii), KMP algorithm

The KMP algorithm is essentially meant to eliminate the extra lookup steps in the lookup. How do you create extra search steps? We can talk with examples. Let's say there are two of the following strings:

A:baaaaabcd

B:aaaab

So what happens when these two are searched? We can look at:

      /* 1 2 3 4 5 6 7 8 9* a:b A A A a    b c d*    B: A A A a   b*       1 2 3 4 5 6 7 8 9*/  
We found that when B and a are compared from the 2nd element, we find that the last element is different, the 6th element of A is a, and the 5th element of B is B. According to the algorithm of the normal string lookup, the following a will continue to move one bit to the right, but in fact 2-5 of the characters we have compared, and 2-5 of these 4 elements are exactly the first 4 elements of B correspond. This time B should be compared with the last element and the 7th element of a. If this calculation step can save, the speed of the search can not be improved?


"Preview: The following blog introduces KMP's writing and multi-check search algorithm"



Step-by-step write algorithm (the string lookup medium)

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.