Introduction to Algorithms
LCS problems:
Description: LCs English: longest common string. Chinese: Longest Common subsequence. That is, find the longest sub-sequence in two strings. For example:
For a D f g C and B F C, the longest sub-sequence is f c.
Given any two strings, we try to provide a general algorithm.
Dynamic Planning is adopted because it meets the characteristics of dynamic planning:
1. Find the optimal problem
2. Sub-problems are independent of each other
3. Sub-problems can overlap, that is, sub-optimal is sub-optimal.
Recursive formula, which references books, is as follows:
When recursion is performed, we always remember two things in our minds:
What are the sub-Problems?
What is the optimal problem?
Sub-problems are the embodiment of program recursion, and the optimum problem is the embodiment of program if
After reading it for a long time, to tell the truth, the methods in "Introduction to algorithms" were really not understood, and combined with the code, I finally got started.
Try to analyze it by yourself. There are not a bunch of formulas in the book, but they are concise and concise.
For example, two strings are given: "B d c a B a a" and "a B c B d a B". The optimal problem is that the string is the longest, in C [I, j], where I, j is the position label of the string; sub problems are: C [I, J-1] and C [I-1, J]. Reference Table Analysis
Combined with the recursive formula, analysis: I = 1 is, take a, followed by j = 1, 2, 3... the comparison is divided into two cases. When I = 1 is also obtained in the row A, the number entered in the cell should be the number in the upper left corner of the diagonal plus 1. Otherwise, compare the numbers on the top and left of the cell, and put them in the cell. For example, at the coordinate (), it corresponds to a and B, which are not equal. If the above and left cells are both 0, enter 0. at coordinate (), a finally touched a. At this time, first check that the cell at coordinate () is 0, then fill in 0 + 1 = 1. the others are similar. arrows represent the source of data. You can trace back when you find the oldest sequence.
Code:
Void findlcs (string str1, string str2) {int M = str1.length (); int n = str2.length (); int C [50] [50]; int mark [50] [50] = {0}; For (INT I = 0; I <m + 1; ++ I) C [I] [0] = 0; for (Int J = 0; j <n + 1; ++ J) C [0] [J] = 0; For (INT I = 1; I <= m; + + I) for (Int J = 1; j <= N; + J) {If (str1 [I-1] = str2 [J-1]) {c [I] [J] = C [I-1] [J-1] + 1; Mark [I] [J] = 1; // representation} else if (C [I-1] [J]> = C [I] [J-1]) {c [I] [J] = C [I-1] [J]; Mark [I] [J] = 2; // representation} else {C [I] [J] = C [I] [J-1]; Mark [I] [J] = 3; // For (INT I = 0; I <= m; ++ I) {for (Int J = 0; j <= N; ++ J) cout <mark [I] [J]; cout <Endl ;}cout <mark [0] <"" <mark [1] <mark; printlcs (mark, M, N, str1 );}
Complexity: O (M * n ).
Print LCS:
void printLCS(int(* a)[50],int r,int c,string str){ if(r==0||c==0) return; if(a[r][c]==1) { printLCS(a,r-1,c-1,str); cout<<str[r-1]; } else if(a[r][c]==2) printLCS(a,r-1,c,str); else printLCS(a,r,c-1,str); }
Complexity: O (m + n)
Test:
int main(){string a="bdcaba";string b="abcbdab";findLCS(b,a); return 0;}
A Dynamic Planning for matrix multiplication is recommended: http://blog.csdn.net/liguisen/article/details/2158127