/**
* @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)的更多相关文章

  1. 动态规划 最长公共子序列 LCS,最长单独递增子序列,最长公共子串

    LCS:给出两个序列S1和S2,求出的这两个序列的最大公共部分S3就是就是S1和S2的最长公共子序列了.公共部分 必须是以相同的顺序出现,但是不必要是连续的. 选出最长公共子序列.对于长度为n的序列, ...

  2. 动态规划----最长公共子序列(LCS)问题

    题目: 求解两个字符串的最长公共子序列.如 AB34C 和 A1BC2   则最长公共子序列为 ABC. 思路分析:可以用dfs深搜,这里使用到了前面没有见到过的双重循环递归.也可以使用动态规划,在建 ...

  3. 动态规划——最长公共子序列LCS及模板

    摘自 https://www.cnblogs.com/hapjin/p/5572483.html 这位大佬写的对理解DP也很有帮助,我就直接摘抄过来了,代码部分来自我做过的题 一,问题描述 给定两个字 ...

  4. 动态规划之最长公共子序列LCS(Longest Common Subsequence)

    一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...

  5. 《算法导论》读书笔记之动态规划—最长公共子序列 & 最长公共子串(LCS)

    From:http://my.oschina.net/leejun2005/blog/117167 1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要 ...

  6. 编程算法 - 最长公共子序列(LCS) 代码(C)

    最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...

  7. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  8. 1006 最长公共子序列Lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...

  9. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  10. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

随机推荐

  1. BZOJ 1096

    const maxm=1e100; maxn=; ..maxn] of int64; q:..maxn] of longint; n,i,h,t:longint; function calc(j,i: ...

  2. leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

    这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...

  3. 获取图片中的文本--MODI

    http://www.aspsnippets.com/Articles/Read-Extract-Text-from-Image-OCR-in-ASPNet-using-C-and-VBNet.asp ...

  4. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  5. android设置按钮按下的不同效果图

    <!-- 按钮设置按下去的不同效果的方式,设置android:background属性, 下面的 button_select实际上是button_select.xml --> <Bu ...

  6. linux命令行后台运行与调回

     直接ctrl+z  这个是暂时到后台执行   要调回来  输入  fg 

  7. Linux学习:netstat命令

    Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态等.对于开发来说,很多时候用于查看端口占用情况. 执行netstat命令,其输出结果可以分成两部分: 1)一是“Active ...

  8. 告别IE给我们的web开发带来的困扰(使用chrome frame v8引擎)

    茶爸爸个人微信:benyzhous,公众号:cha-baba欢迎骚扰 由于客户所有机器必须使用IE6浏览器,导致我们在开发项目过程中遇到非常多的样式与性能问题,在偶然的一次使用360软件管家搜索chr ...

  9. B-树和B+树的应用:数据搜索和数据库索引

    B-树和B+树的应用:数据搜索和数据库索引  B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每 ...

  10. Open vswitch 之Qos rate-limiting 原理

    Openvswitch之Qos rate-limiting原理 OVS的qosrate-limiting功能是采用令牌桶(Token-Bucket)机制进行的.这里的“令牌桶”是指网络设备的内部存储池 ...