/**
* @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. linux内核源码阅读之facebook硬盘加速flashcache之二

    flashcache数据结构都在flashcache.h文件中,但在看数据结构之前,需要先过一遍flashcache是什么,要完成哪些功能?如果是自己设计这样一个系统的话,大概要怎么设计. 前面讲过, ...

  2. Struts2 学习笔记19 类型转换 Part1

    现在来说一说类型转换,提到类型转换其实我们之前早已经用过了,在url传递参数的时候,我们传递过来的参数其实都是String类型的,在显示的时候都自动转换了,像这种简单的转换很好理解,我们要说的是,转换 ...

  3. UITextField align left margin

    如果我们想让我们的UITextField输入的字体偏移几个像素,我们常常用空格来代替,有时候我们不想用空格的话怎么办? #import <UIKit/UIKit.h> @interface ...

  4. leetcode_question_67 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  5. 信号量多-threaded同步Semaphore

    Semaphore它是JDK1.5一个实现后,外面有个办法同步.Semaphore能够保持其当前的线程接入号码.并提供了一个同步机制. 采用Semaphore时,可以用相同的对资源的访问进行控制的线程 ...

  6. CAEmitterLayer 粒子发射器

    在iOS 5中,苹果引入了一个新的CALayer子类叫做CAEmitterLayer.CAEmitterLayer是一个高性能的粒子引擎,被用来创建实时例子动画如:烟雾,火,雨等等这些效果. CAEm ...

  7. 一些指令 & 一些知识 (Linux Spring log4j...)

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  8. nginx下配置二级域名指向子目录

    今天终于把nginx的二级域名配置搞定了,哎之前在测试服务器上弄过一次,不过那个是在本地解析的hosts,把ip指向到域名上就ok,再在nginx.conf里改了下配置就好了,用同样的方法改了正式服务 ...

  9. python list comprehension twos for loop 嵌套for循环

    list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...

  10. ALEXANDER WANG 北京旗舰店开业活动

    ALEXANDER WANG 北京旗舰店开业活动-搜狐女人 ALEXANDER WANG 北京旗舰店开业活动