Longest Common subsequence

Source: Internet
Author: User
I. algorithm ideas
The subsequence of a given sequence is obtained after several elements are deleted from the sequence. Given two sequences X and Y, when another sequence Z is both a subsequence of X and a subsequence of Y, it is called Z as a public subsequence of the sequences X and Y. The longest common subsequence is the longest common subsequence given two sequences. Dynamic Planning can effectively solve this problem. Based on the optimal sub-structure of the sub-sequence of the longest common sub-sequence problem, the optimal recursive relationship of the sub-problem can be established. Use c [I] [j] to record the length of the longest common subsequence of sequence Xi and Yi. The recursive relationship is as follows:
0 I = 0, j = 0
C [I] [j] = c [I-1] [j] [J-1] + 1 I, j> 0; xi = yj
Max c [I] [J-1], c [I-1] [j] I, j> 0; xi = yj
In the specific algorithm design, X = {x1, x2, x3 ,..., Xm} and Y = {y1, y2, y3 ,..., Ym} is used as the input. Output three arrays c, B, and temp. Where c [I] [j] stores the length of the common subsequences of Xi and Yj, the value of B [I] [j] records c [I] [j] is obtained from the solution of a subproblem, which is used to construct the longest common subsequence. The optimal solution is that the longest common subsequences of X and Y are recorded in temp [h.
Ii. Source Code
The following is the source program for finding the longest common subsequence written in Microsoft Visual C ++ 6.0. The program defines the maximum string length as 99, the Dynamic Planning Algorithm on p48 page is rewritten.
# Include <iostream. h>
# Include <iomanip. h>
# Define MAX 99
// Typedef char MM;
Void main ()
{Int I, j, m, n, h = 0;
Char x [MAX] = {'',''}, y [MAX] = {'',''}, B [MAX] [MAX] = {''};
Int c [MAX] [MAX] = {0 };
Char temp [MAX] = {''};
Cout <"** this program can obtain the maximum public subsequence of any two strings with less than 99 characters ** \ n ";
Cout <"Enter the length of the first string m = ";
Cin> m;
Cout <"Enter the first string (" Press ENTER "to end) \ n. If the number of characters entered exceeds m, an error occurs! \ Nx ["<m <"] = ";
For (I = 1; I <= m; I ++)
Cin> x [I]; // enter x and y on the keyboard.
Cout <"Enter the length of the second string n = ";
Cin> n;
Cout <"enter the second string \ ny [" <n <"] = ";
For (I = 1; I <= n; I ++)
Cin> y [I];
For (I = 1; I <= m; I ++) c [I] [0] = 0; // start of Dynamic Planning
For (I = 1; I <= n; I ++) c [0] [I] = 0;
For (I = 1; I <= m; I ++)
For (j = 1; j <= n; j ++)
{If (x [I] = y [j])
{C [I] [j] = c [I-1] [J-1] + 1;
B [I] [j] = '\\';
} Else
If (c [I-1] [j]> = c [I] [J-1])
{C [I] [j] = c [I-1] [j];
B [I] [j] = '│ ';
} Else {c [I] [j] = c [I] [J-1];
B [I] [j] = '-';
}
} // End of Dynamic Planning
Cout <"c [m] [n] content: \ n ";
For (I = 0; I <= m; I ++)
{For (j = 0; j <= n; j ++)
Cout <c [I] [j];
Cout <endl;
}
Cout <"B [m] [n] content: \ n ";
For (I = 1; I <= m; I ++)
{For (j = 1; j <= n; j ++)
Cout <B [I] [j];
Cout <endl;
}
I = m, j = n;
While (1)
{If (I = 0 │ j = 0) break;
If (B [I] [j] = '\\'){
Temp [h ++] = x [I]; // records the longest common subsequence to temp in reverse order.
I = I-1, j = J-1;
}
Else
If (B [I] [j] = '│ ')
I = I-1;
Else
J = J-1 ;}
The longest common subsequences of cout <"\ nx [" <m <"] AND y [" <n <"] are :";
For (I = h-1; I> = 0; I --) // format the output Longest Common subsequence
If (I = h-1)
If (h = 1)
Cout <"LCS: <" <temp [I] <"> ";
Else
Cout <"LCS: <" <temp [I];
Else
If (I = 0)
Cout <"," <temp [I] <"> ";
Else
Cout <"," <temp [I];
Cout <"\ n" <endl;
}
Iii. Calculation Result
In fact, it has more than one Longest Common subsequence, and only one of them is output here.
Iv. Summary and Analysis
There are also some improvements in this specific algorithm. For example, in the specific maximization of Public subsequences, the macro definition of MAX can be unnecessary, you only need to set the length of each array to save a lot of space and greatly reduce the space complexity of the program. However, to input arbitrary strings on the keyboard, a lot of memory space is sacrificed. When inputting a string on the keyboard, you can directly use cin> x; cin> y without repeating the value assignment. In this way, the time complexity of this part can be changed from O (m + n) to O (2), but there is a problem that cannot be solved, so I did not assign this value. The total time complexity of the program is O (mn + 3 m + 3n ).

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.