Longest Common subsequence and public subsequence

Source: Internet
Author: User

Longest Common subsequence and public subsequence

Description
Subsequence refers to the sequence obtained after several (0) elements are deleted from a sequence. To be accurate, if the given sequence X = (x1, x2, ·, xm), then the other sequence Z = (z1, z2, ·, zk ), the subsequence of X refers to the existence of a strictly incrementing subscript sequence (i1, i2, ·, ik) so that for all j = 1, 2 ,···, k has zj = xij. For example, sequence Z = (B, C, D, B) is A subsequence of sequence X = (A, B, C, B, D, A, B, the corresponding ascending subscript sequence is (1, 2, 4, 6 ). Given two sequences X and Y, obtain the longest common subsequence of X and Y ).

Input
The input includes multiple groups of test data. Each group occupies one row and contains two strings (the string length cannot exceed 200), representing two sequences. The two strings are separated by several spaces.

Output
For each group of test data, the maximum length of the Public subsequence is output, and each group has one row.
Sample Input
Abcfbc abfcab
Programming contest
Abcd mnp
Sample output
4
2
0
Analysis
The longest common subsequence problem has the optimal substructure. Set the longest common subsequences of X = (x1, x2, ·, xm) and Y = (y1, y2, ·, yn) to Z = (z1, z2,..., zk), then
• If xm = yn, zk = xm = yn, and Zk-1 is the longest common subsequence of Xm-1 and Yn-1.
• If xm is less than yn and zk is less than xm, Z is the longest common subsequence of Xm-1 and Y.
• If xm =yn and zk =yn, Z is the longest common subsequence of X and yn-1.

Among them, Xm-1 = (x1, x2,..., xm-1), Yn-1 = (y1, y2,..., yn-1 ), zk − 1 = (z1, z2,..., zk − 1 ).
Set the state to d [I] [j], which indicates the length of the longest common subsequence of sequence Xi and Yj. Available from the optimal sub-structure
The state transition equation is as follows:

0 I = 0, j = 0

D [I] [j] = {d [I −1] [j −1] + 1 I, j> 0; xi = yi

Max {d [I] [j −1], d [I −1] [j]} I, j> 0; xi =yi

If you want to print the longest common subsequence, you need to set an array p, p [I] [j] record d [I] [j] Which subproblem is obtained.

Implementation Code

1 # include <stdio. h> 2 # include <string. h> 3 # define MAX 201 4/* the maximum length of a string is 200 */5 6 int d [MAX] [MAX]; /* d [I] [j] indicates the length of the longest common subsequence of sequence Xi and Yj */7 char x [MAX], y [MAX]; /* the string ends with a '0' */8 9 void lcs (const char * x, const int m, const char * y, const int n) 10 {11 int I, j; 12 for (I = 0; I <= m; I ++) 13 d [I] [0] = 0; 14 for (j = 0; j <= n; j ++) 15 d [0] [j] = 0; 16 17/* boundary initialization */18 for (I = 1; I <= m; I ++) 19 {20 for (j = 1; j <= n; j ++) 21 {22 if (x [I-1] = y [J-1]) 23 {24 d [I] [j] = d [I-1] [J-1] + 1; 25} else {26 d [I] [j] = d [I-1] [j]> d [I] [J-1]? D [I-1] [j]: d [I] [J-1]; 27} 28} 29} 30} 31 32 void lcs_extend (const char * x, const int m, const char * y, const int n); 33 void lcs_print (const char * x, const int m, const char * y, const int n); 34 35 int main () 36 {37 while (scanf ("% s", x, y )! = EOF) 38 {39 const int lx = strlen (x); 40 const int ly = strlen (y); 41 lcs (x, lx, y, ly ); 42 printf ("% d \ n", d [lx] [ly]); 43} 44 return 0; 45} 46 47 int p [MAX] [MAX]; /* p [I] [j] records d [I] [j] Which subproblem is obtained */48 void lcs_extend (const char * x, const int m, const char * y, const int n) 49 {50 int I, j; 51 memset (p, 0, sizeof (p); 52 for (I = 0; I <= m; I ++) 53 d [I] [0] = 0; 54 for (j = 0; j <= n; j ++) 55 d [0] [j] = 0; 56 57/* boundary initialization */58 for (I = 1; I <= m; I ++) 59 {60 for (j = 1; j <= n; j ++) 61 {62 if (x [I-1] = y [J-1]) 63 {64 d [I] [j] = d [I-1] [J-1] + 1; 65 p [I] [j] = 1; 66} 67 else 68 {69 if (d [I-1] [j]> = d [I] [J-1]) 70 {71 d [I] [j] = d [I-1] [j]; 72 p [I] [j] = 2; 73} 74 else 75 {76 d [I] [j] = d [I] [J-1]; 77 p [I] [j] = 3; 78} 79} 80} 81} 82} 83 84 void lcs_print (const char * x, const int m, const char * y, const int n) 85 {86 if (m = 0 | n = 0) 87 return; 88 if (p [m] [n] = 1) 89 {90 lcs_print (x, m-1, y, n-1); 91 printf ("% c", x [m-1]); 92} 93 else if (p [m] [n] = 2) 94 {95 lcs_print (x, m-1, y, n ); 96} 97 else 98 {99 lcs_print (x, m, y, n-1); 100} 101}

Related Article

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.