动态规划——最长公共子序列(LCS)
/**
* @brief longest common subsequence(LCS)
* @author An
* @data 2013.8.26
**/ #include <iostream>
#include <string>
using namespace std; enum Direction { Zero, LeftUp, Up, Left };
static int m; // length of the first sequence
static int n; // length of the second sequence
static int **c; // the length for every subsequence
static Direction **b; // record the path void LCS_Length( string x, string y );
void Print_LCS( string x, int i, int j );
void PrintTable(); int main()
{
string x = "ABCBDAB";
string y = "BDCABA";
LCS_Length( x, y );
Print_LCS( x, m, n );
cout << endl;
PrintTable();
} void LCS_Length( string x, string y )
{
// initialize two tables
m = x.length();
n = y.length();
c = new int*[m + 1];
b = new Direction*[m + 1];
for ( int i = 0; i <= m; ++i )
{
c[i] = new int[n + 1];
b[i] = new Direction[n + 1];
} // zero row and column
for ( int i = 0; i <= m; ++i )
{
c[i][0] = 0;
b[i][0] = Zero;
}
for ( int j = 1; j <= n; ++j )
{
c[0][j] = 0;
b[0][j] = Zero;
} // calculate the two tables from bottom to top
for ( int i = 1; i <= m; ++i )
{
for ( int j = 1; j <= n; ++j )
{
if ( x[i - 1] == y[j - 1] )
{
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = LeftUp;
}
else if ( c[i - 1][j] >= c[i][j - 1] )
{
c[i][j] = c[i - 1][j];
b[i][j] = Up;
}
else
{
c[i][j] = c[i][j - 1];
b[i][j] = Left;
}
} // end for
} //end for } // end LCS_Length() void Print_LCS( string x, int i, int j )
{
if ( i == 0 || j == 0 )
{
return;
}
if ( b[i][j] == LeftUp )
{
Print_LCS( x, i - 1, j - 1 );
cout << x[i - 1];
}
else if ( b[i][j] == Up )
{
Print_LCS( x, i - 1, j );
}
else
{
Print_LCS( x, i, j - 1 );
}
} void PrintTable()
{
for ( int i = 0; i <= m; ++i )
{
for ( int j = 0; j <= n; ++j )
{
cout << c[i][j] << " ";
}
cout << endl;
}
cout << endl;
for ( int i = 0; i <= m; ++i )
{
for ( int j = 0; j <= n; ++j )
{
cout << b[i][j] << " ";
}
cout << endl;
}
}
动态规划——最长公共子序列(LCS)的更多相关文章
- 动态规划 最长公共子序列 LCS,最长单独递增子序列,最长公共子串
LCS:给出两个序列S1和S2,求出的这两个序列的最大公共部分S3就是就是S1和S2的最长公共子序列了.公共部分 必须是以相同的顺序出现,但是不必要是连续的. 选出最长公共子序列.对于长度为n的序列, ...
- 动态规划----最长公共子序列(LCS)问题
题目: 求解两个字符串的最长公共子序列.如 AB34C 和 A1BC2 则最长公共子序列为 ABC. 思路分析:可以用dfs深搜,这里使用到了前面没有见到过的双重循环递归.也可以使用动态规划,在建 ...
- 动态规划——最长公共子序列LCS及模板
摘自 https://www.cnblogs.com/hapjin/p/5572483.html 这位大佬写的对理解DP也很有帮助,我就直接摘抄过来了,代码部分来自我做过的题 一,问题描述 给定两个字 ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 《算法导论》读书笔记之动态规划—最长公共子序列 & 最长公共子串(LCS)
From:http://my.oschina.net/leejun2005/blog/117167 1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要 ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- WPF 自定义TextBox
1.TextBox前加图标. 效果: <TextBox Width="300" Height="30" Style="{StaticResour ...
- UIPickView之自定义生日键盘和城市键盘
//// ViewController.m// 04-键盘处理// // #import "ViewController.h"#import "XMGProvince ...
- vector数据查找方法
用STL编敲代码时常常使用vector容器来存储数据.当容器中的数据有序时我们能够採取两种方式: (1) 利用<algorithm>中的find函数进行查找: (2) 折半查找. 另外也能 ...
- LKD3
第三章 进程1. Unix操作系统的抽象:进程和文件2. 进程包括两个因素:可运行代码,和资源(打开的文件,挂起的信号,内核内部数据,处理器状态,地址空间)3. 线程是进程中活动的对象.4. 线程有独 ...
- UVA 10312 - Expression Bracketing(数论+Catalan数)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1253">10312 - Exp ...
- c 整数的逆序输出 输入3,2就算 2+22+222的结果
#include<stdio.h> #include<math.h> //整数逆序输出 void nixu() { int num,i; i = ; scanf("% ...
- Codeforces 492B B. Vanya and Lanterns
Codeforces 492B B. Vanya and Lanterns 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- Python网络编程——通过指定的端口和协议找到服务名
1.通过指定的端口和协议找到对应的服务名,采用socket中getservbyprot()函数实现. import socket def find_service_name(): protocolna ...
- 转:shell比较两个字符串是否相等
比较两个字符串是否相等的办法是: if [ "$test"x = "test"x ]; then这里的关键有几点:1 使用单个等号2 注意到等号两边各有一个空格 ...
- 编译:一个 C 程序的艺术之旅(转载)
C 程序为什么要编译才能执行?一个 C 程序在变成可执行文件的过程中,为什么要经过预处理.编译.汇编.链接这四道工序?让我们从这段简单的 C 程序开始. 为什么要编译 这并不是一个简单的问题.我们知道 ...