Longest Common subsequence (LCS) and longest sequence lcs

Source: Internet
Author: User

Longest Common subsequence (LCS) and longest sequence lcs

[Description]: two strings s1s2... are given ...... Sn and t1 t2 ...... Tn. Find the longest length of the two strings that are common to your sub-sequence. String s1 s2 ...... The sub-sequence of sn indicates that it can be expressed as si1 i2 ...... Si n (i1 <i2 <i3 <...... <Im ).

Example:

N = 4;

M = 4

S = "abcd"

T = "becd"

Output:

3 (bcd)

[Analysis]: We can use dynamic planning to solve this classic longest public subsequence problem. We will use the following methods to define it.

Dp [I] [j] indicates the length of the longest common subsequence of the two output sequences.

Therefore, the common subcolumns of the two subsequences may be:

1. When the I + 1 element of the S sequence is the same as the I + 1 element of the T sequence ...... Si and T1 ...... Add S (I + 1) to the end of the public subcolumn OF Ti)

2. s1 ...... Si and t1 ...... T (j + 1.

3. s1 ...... S (I + 1) and t1 ...... Tj public subcolumns.

One of the three, so we can write the recursive relationship of dp:


That is:

#include<iostream>using namespace std;int n,m;char s[1000],t[1000];int dp[1001][1001];int main(){cin>>n>>m;for(int i = 0;i<n;i++)cin>>s[i];for(int j = 0;j<m;j++)cin>>t[j];for(int i = 0;i<n;i++){for(int j = 0;j<m;j++){if(s[i] == t[j])dp[i+1][j+1] = dp[i][j] + 1;elsedp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);}}cout<<dp[n][m]<<endl;return 0;}



PASCAL Implementation of LCS (longest common subsequence)

Sorry, I have previously given the longest public substring. Do you want to ask this question? Problem: "We know that ALRIs and his younger brother alria have basically the same genes. it is inconvenient to express these genes, so it is represented by n numbers. (Since 100000 genes have been found so far, each number <= 100000) has the same number of genes, that is, they all have n numbers. And for each person, the n numbers are different from each other. The longest common part of genes between siblings is required. It can be discontinuous ."
Line 2 of the Input file LCS. IN is n (1 <= n <= 1st) and n numbers IN each row, indicating all genes of a person.
The Output file LCS. OUT contains the length of the longest common part of the two genes.
Sample Input Sample Output
7 3
1 2 3 4 5 6 7
7 6 5 4 1 2 3
Program for you
Var n, I, j: longint;
A, B, numa, numb: array [1 .. 100000] of longint;

Procedure qsrta (h, t: longint );
Var I, j, k, x, y: longint;
Begin
If h> = t then exit;
K: = random (t-h + 1) + h;
X: = a [k]; a [k]: = a [h]; a [h]: = x;
Y: = numa [k]; numa [k]: = numa [h]; numa [h]: = y;
I: = h; j: = t;
While I <j do begin
While (I <j) and (a [j]> x) do dec (j );
If I <j then begin a [I]: = a [j]; numa [I]: = numa [j]; inc (I); end;
While (I <j) and (a [I] <x) do inc (I );
If I <j then begin a [j]: = a [I]; numa [j]: = numa [I]; dec (j); end;
End;
A [I]: = x; numa [I]: = y;
Qsrta (h, I-1); qsrta (I + 1, t );
End;

Procedure qsrtb (h, t: longint );
Var I, j, k, x, y: longint;
Begin
If h> = t then exit;
K: = random (t-h + 1) + h;
X: = B [k]; B [k]: = B [h]; B [h]: = x;
Y: = numb [k]; numb [k]: = numb [h]; numb [h]: = y;
I: = h; j: = t;
While I <j do begin
While (I <j) and (B [j]> x) do dec (j );
If I <j then begin B [I]: = B [j]; numb [I]: = numb [j]; inc (I); end;
While (I <j) and (B [I] <x) do inc (I );
If I <j then begin B [j]: = B [I]; numb [j]: = numb [I]; dec (j); end;
End;
B [I]: = x; numb [I]: = y;
Qsrtb (h, I-1); qsrtb (I + 1, t );
End;

Procedure qsrtnumab (h, t: longint );
Var ...... remaining full text>

Matlab recursion solves the longest common subsequence (LCS) Problem

Recursion is not good, and time is slow; recursion is VC. MATLAB is different from VC, and it uses matrices for calculation;
 

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.