动态规划——最长公共子序列(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的最长公共子序列(子序列不要求是连续的). ...
随机推荐
- ThinkPHP 3.1.2 模板中的基本语法<1>
# # ThinkPHP 3.1.2 模板中的基本语法 一.传统的方式,导入CSS和JS文件 1.css link js scr <link rel='stylesheet' type='tex ...
- BZOJ 3211 花神游历各国 (树状数组+并查集)
题解:首先,单点修改求区间和可以用树状数组实现,因为开平方很耗时间,所以在这个方面可以优化,我们知道,开平方开几次之后数字就会等于1 ,所以,用数组记录下一个应该开的数,每次直接跳到下一个不是1的数字 ...
- Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project
Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project Qt WebKit and HTML5 geolocation I ...
- WeasyPrint - Converts HTML + CSS to PDF - WeasyPrint converts HTML/CSS documents to PDF
WeasyPrint - Converts HTML + CSS to PDF - WeasyPrint converts HTML/CSS documents to PDF WeasyPrint c ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- Objective-c 程序结构
类是Objective-c的核心,Objective-c程序都是围绕类进行的.Objective-c程序至少包含以下三个部分: 1.类接口:定义了类的数据和方法,但是不包括方法的实现代码. 2.类实现 ...
- Python 参数传递
python中的变量: 一个变量是局部还是全局,在编译函数的时候就已经决定,因此读变量值的时候也不会逐层向外查找.变量是全局还是局域,根据如下3条: 1. 如果函数内部有global语句,那么它声明的 ...
- sql server中的系统数据库
1.master数据库 master是SQL Server中最重要的数据库,是整个数据库服务器的核心.用户不能直接修改该数据库,如果损坏了master数据库,整个SQL Server服务器将不能工作. ...
- Oracle游标-循环查询表中数据(表名),并执行
Oralce 表中存有一个字段,该字段存储表名,要把该表中的所有表名查询出来(即表名结果集),且执行结果集from 表名结果集: declare v_ccount ); --定义一个游标变量 curs ...
- Debian(Linux)系统目录简单说明
bin:基础命令执行档 boot:引导装置器的静态链接文件 dev:设备档 etc:主机特定的系统配置 lib:基本共享库及基本内核模块 mnt:用于临时挂载一个文件系统 proc:系统信息的虚拟目录 ...