LCS (Longest Common Subsequence)

LCS是两个序列相似性的一种度量方法;

若序列s1:2,5,7,9,3,1,2

s2:3,5,3,2,8

则LCS为:5,3,2

思路可参考:http://www.csie.ntnu.edu.tw/~u91029/LongestCommonSubsequence.html

具体代码实现为:

 1 #include<iostream>
 2 using namespace std;
 3
 4 int s1[7+1]={0,2,5,7,9,3,1,2};
 5 int s2[7+1]={0,3,5,3,2,8};
 6 const int s1_length=8;
 7 const int s2_length=6;
 8 const int LEFT=1;
 9 const int UP=2;
10 const int UP_LEFT=3;
11 const int LEFTORUP=4;
12
13 int arr[7+1][5+1]={0};
14 int pre[7+1][5+1]={0};
15
16 void LCS();
17 void print_LCS(int i,int j);
18
19 int main()
20 {
21     cout<<"the LCS of the sequence is:";
22     LCS();
23     return 0;
24 }
25
26 void LCS()
27 {
28     for(int i=0;i<s2_length;++i)
29         arr[0][i]=0;
30     for(int i=0;i<s1_length;++i)
31         arr[i][0]=0;
32
33     for(int i=1;i<s1_length;++i)
34         for(int j=1;j<s2_length;++j)
35             if(s1[i]==s2[j])
36             {
37                 arr[i][j]=arr[i-1][j-1]+1;
38                 pre[i][j]=UP_LEFT;
39             }
40             else
41             {
42                 if(arr[i-1][j]==arr[i][j-1])
43                 {
44                     arr[i][j]=arr[i-1][j];
45                     pre[i][j]=LEFTORUP;
46                 }
47                 else if(arr[i-1][j]>arr[i][j-1])
48                 {
49                     arr[i][j]=arr[i-1][j];
50                     pre[i][j]=UP;
51                 }
52                 else
53                 {
54                     arr[i][j]=arr[i][j-1];
55                     pre[i][j]=LEFT;
56                 }
57             }
58
59     print_LCS(s1_length-1,s2_length-1);
60 }
61
62 void print_LCS(int i,int j)
63 {
64     if(i==0||j==0)
65         return;
66
67     if(pre[i][j]==UP_LEFT)
68     {
69         cout<<s1[i]<<‘\t‘;
70         print_LCS(i-1,j-1);
71     }
72     else if(pre[i][j]==UP)
73         print_LCS(i-1,j);
74     else if(pre[i][j]==LEFT)
75         print_LCS(i,j-1);
76 }

以上情况中两个序列只存在一个LCS,但是若存在多个LCS应该怎么解决呢?

若序列s1:2,5,7,9,3,1,2

s2:3,5,3,2,8,7,9

则LCS为:5,3,2; 2,7,9; 5,7,9

以上两个表格为算法的思路,上面表格为顺序思路,arr[i][j]表示s1(1:i) s2(1:j)两个序列的LCS长度,下面表格为回溯思路,从右下角开始根据方向得到LCS,其中数字意义为:

LEFT=1;
 UP=2;
 UP_LEFT=3;
 LEFTORUP=4;顺序时由上面或左面数字得到(区别于只有一个LCS存在),目的在于回溯时可以得到序列所有的LCS,代码还没有实现。

LCS (Longest Common Subsequence)

时间: 2024-08-09 02:50:51

LCS (Longest Common Subsequence)的相关文章

【算法导论学习-29】动态规划经典问题02:最长公共子序列问题(Longest common subsequence,LCS)

问题描述:序列X={x1,x2,-,xn},Y={y1,y2,-,yn},当Z={z1,z2-,zn}是X的严格递增下标顺序(可以不连续)的子集,也是Y的严格递增下标顺序(可以不连续)的子集,则Z是X和Y的公共子序列.例如X={A,B,C,B,D,A,B},Y={B,D,C,A,B,A},{B,C,A}.{B,C,B,A}.{B,D,A,B}都是X和Y的公共子序列.其中最长的公共子序列叫做Longest common subsequence,即经典的LCS. 具体点:char[]xArray和c

AOJ_ALDS1_10_C Longest Common Subsequence【LCS+DP】

Longest Common Subsequence Aizu - ALDS1_10_C For given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y. For example, if X={a,b,c,b,d,a,b} and Y={b,d,c,a,b,a}, the sequence {b,c,a} is a comm

UVA10405 Longest Common Subsequence【LCS+DP】

Given two sequences of characters, print the length of the longest common subsequence of both sequences. ????Sequence 1: ????Sequence 2: ????For example, the longest common subsequence of the following two sequences 'abcdgh' ans 'aedfhr' is 'adh' of

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

[HackerRank] The Longest Common Subsequence

This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here. My accepted code is as follows. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5

[Algorithms] Longest Common Subsequence

The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t. Do you know the difference between substring and subequence? Well, substring i

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

Longest Common Subsequence Accepted : 7 Submit : 66 Time Limit : 3000 MS Memory Limit : 65536 KB Longest Common Subsequence Bobo has a sequence A=(a1,a2,-,an) of length n. He would like to know f(0),f(1),f(2) and f(3) where f(k) denotes the number of