[Practice] Boyer-Moore and longest common substring (LCS) for algorithm Learning)

Source: Internet
Author: User



Yesterday's Problem
Solution 1: Find the hash function, which is extremely feasible.
Solution 2: load the memory and maintain it as a daemon service. Difficult.
Solution 3: Use the first five digits for the index, which increases from the first three digits to the first five digits for uniqueness. Theoretically, the split record is increased by 100 times, but MySQL can be used locally, which is the easiest way.
Solution 4: Use solution 3, but add a table to reduce redundancy. However, a new table is created at the cost and two tables are joined in select join for each query.




This paper studies the longest common substring problem and string matching by the way.

Boyer-Moore algorithm for String Matching
Http://www.ruanyifeng.com/blog/2013/05/boyer-moore_string_search_algorithm.html
String Matching KMP Algorithm
Http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

Dynamic Planning Algorithm: Longest Common subsequence & Longest Common substring (LCS)
Http://my.oschina.net/leejun2005/blog/117167

Longest public substring

In fact, this is a sequential decision-making problem, which can be solved using dynamic planning. We use a two-dimensional matrix to record the intermediate results. How to construct this two-dimensional matrix? For example: "Bab" and "Caba" (of course, we can see at a glance that the longest public substring is "ba" or "AB ")

B A B

C 0 0 0

A 0 1 0

B 1 0 1

A 0 1 0

We can see that the longest diagonal line of the matrix can find the longest common substring.

However, finding the longest diagonal line composed of 1 on a two-dimensional matrix is also time-consuming. The following improvements: when the matrix is filled with 1, make it equal to the element in the upper left corner of the matrix plus 1.

B A B

C 0 0 0

A 0 1 0

B 1 0 2

A 0 2 0

In this way, the maximum element in the matrix is the length of the longest common substring.

In the process of constructing the two-dimensional matrix, the previous row of the matrix is useless because a row of the matrix is obtained. In fact, the one-dimensional array can be used in the program to replace the matrix.



Based on the above Algorithm
I practiced it in C language.

# Include <stdlib. h> # include <stdio. h> # include <string. h> int comfix (const char * stra, const char * strb); int main (void) {const char * stra = "Hello World", * strb = "malloc "; printf ("% s, % s: % d \ n", stra, strb, comfix (stra, strb); Return 0;} int comfix (const char * stra, const char * strb) {/** variable first character * C: char *, L: Len * variable second character * s: small, l: large */const char * cs = stra, * Cl = strb; int ret = 0, La = strlen (stra), lB = strlen (strb), ls = La, LL = LB;/* If not, change it */If (Lb <la) Cs = strb, ls = LB, CL = stra, LL = La;/* matrix, save only one row of the matrix. It can be dynamic */int * pint = (int *) malloc (LS + 1) * 4); memset (pint, 0, (LS + 1) * 4); int I, j; for (I = 0; I <ll; I ++) {/* generates the next row, at the same time, the content of the last row is recycled */For (j = ls; j> ret; j --) if (cl [I] = cs [J]) pint [J] = pint [J-1] + 1;/* update RET if bigger */For (j = ls; j> ret; j --) if (pint [J]> RET) ret = pint [J];} return ret ;}

 




This kind of algorithm is cleverly simplified, and sometimes it will become more and more cheerful if you change your mind!
I prefer this kind of exercise.

 

[Practice] Boyer-Moore and longest common substring (LCS) for algorithm Learning)

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.