LCS (Longest Common Subsequence), lcssubsequence

Source: Internet
Author: User

LCS (Longest Common Subsequence), lcssubsequence
Problem description
Longest Common subsequence, abbreviated as LCS (Longest Com # include <bits/stdc ++. h> const int MAX = 1010; char x [MAX]; char y [MAX]; int DP [MAX] [MAX]; int B [MAX] [MAX]; using namespace std; int PRINT_LCS (int B [] [MAX], char * x, int I, int j) {if (I = 0 | j = 0) return 1; if (B [I] [j] = 1) {PRINT_LCS (B, x, I-1, J-1 ); cout <x [I] <";} else if (B [I] [j] = 2) {PRINT_LCS (B, x, I-1, j );} else if (B [I] [j] = 3) {PRINT_LCS (B, x, I, J-1) ;}} int main () {int T; int n, m, I, j; cin> T; while (T --) {while (cin> n> m) {for (int I = 1; I <= n; I ++) cin> x [I]; for (int j = 1; j <= m; j ++) cin> y [j]; memset (DP, 0, sizeof (DP); for (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) {if (x [I] ==y [j]) {DP [I] [j] = DP [I-1] [J-1] + 1; B [I] [j] = 1;} else if (DP [I-1] [j]> = DP [I] [J-1]) {DP [I] [j] = DP [I-1] [j]; B [I] [j] = 2 ;} else {DP [I] [j] = DP [I] [J-1]; // Max (DP [I-1] [j], DP [I] [J-1]); B [I] [j] = 3 ;}} cout <DP [n] [m] <endl; PRINT_LCS (B, x, n, m ); cout <endl ;}} return 0 ;}View Code

 

Mon Subsequence ). It is defined as a sequence S. If it is a subsequence of two or more known sequences, and it is the longest of all sequences that meet this condition, S is the longest common subsequence of known sequences. The longest public substrings (requiring continuous) and longest public substrings are different applications.
The longest common subsequence is a very practical problem. It can describe the "similarity" between two paragraphs, that is, their similarity, which can be used to identify plagiarism. After a paragraph of text is modified, the longest common subsequence of the text before and after the change is calculated, and the parts other than the subsequence are extracted. This method is usually very accurate in determining the modified part. In short, Baidu knows and Baidu encyclopedia can be used.

Dynamic Planning

Step 1: Calculate the length of the longest common subsequence.

Step 2: Calculate the longest common subsequence Based on the length and use backtracing.

There are two sequences X = {x1, x2, x3,... xi}, Y = {y1, y2, y3,..., yi },

Set a C [I, j]: Save the length of the LCS of Xi and Yj.

The recursive equation is:

Code test:

 

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.