问题描述:

给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列。(子序列中的字符不要求连续)

这道题可以用动态规划解决。定义c[i, j]表示Xi和Yj的LCS的长度,可得如下公式:

伪代码如下:

C++实现:

int longestCommonSubsequence(string x, string y)
{
int m = x.length();
int n = y.length();
vector< vector<int> > c(m + , vector<int>(n + )); for (int i = 0; i <= m; ++i)
c[i][] = ;
for (int j = 1; j <= n; ++j)
c[][j] = ;
for (int i = ; i <= m; ++i)
{
for (int j = ; j <= n; ++j)
{
if (x[i-] == y[j-])
c[i][j] = c[i-][j-] + ;
else if (c[i-][j] >= c[i][j-])
c[i][j] = c[i-][j];
else
c[i][j] = c[i][j-];
}
}
return c[m][n];
}

后记:

我本来以为我已经掌握了LCS,其实不过是记住了LCS的状态转移方程。15号参加了创新工场2016校园招聘笔试,题目要求打印出LCS,我就懵逼了。其实《算法导论》里讲的清清楚楚啊。

贴一下我的C++实现:

vector< vector<int> > b;    //辅助数组
void LCS(string x, string y)
{
int m = x.length();
int n = y.length(); vector< vector<int> > c(m + , vector<int>(n + ));
for (int i = ; i <= m; ++i)
c[i][] = ;
for (int j = ; j <= n; ++j)
c[][j] = ; b.resize(m+);
for (int i = ; i <= m; i++)
{
b[i].resize(n+);
}
for (int i = ; i <= m; i++)
for (int j = ; j <= n; j++)
{
b[i][j] = ;
} for (int i = ; i <= m; ++i)
{
for (int j = ; j <= n; ++j)
{
if (x[i-] == y[j-])
{
c[i][j] = c[i-][j-] + ;
b[i][j] = ; //
}
else if (c[i-][j] >= c[i][j-])
{
c[i][j] = c[i-][j];
b[i][j] = ; //
}
else
{
c[i][j] = c[i][j-];
b[i][j] = ; //
}
}
} } void printLCS(vector< vector<int> > &b, string x, int i, int j)
{
if (i == || j == )
return ;
if (b[i][j] == )
{
printLCS(b, x, i-, j-);
printf("%c", x[i-]);
}
else if (b[i][j] == )
printLCS(b, x, i-, j);
else
printLCS(b, x, i, j-); }

阿里、腾讯、创新工场全部笔试跪;网易互联网简历挂;明天去面百度和华为,加油!!!

最长公共子序列(Longest common subsequence)的更多相关文章

  1. 算法实践--最长公共子序列(Longest Common Subsquence)

    什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...

  2. 最长公共子串(Longest common substring)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...

  3. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

  4. 利用后缀数组(suffix array)求最长公共子串(longest common substring)

    摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...

  5. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  6. 动态规划--最长上升子序列(Longest increasing subsequence)

    前面写了最长公共子序列的问题.然后再加上自身对动态规划的理解,真到简单的DP问题很快就解决了.其实只要理解了动态规划的本质,那么再有针对性的去做这方的题目,思路很快就会有了.不错不错~加油 题目描述: ...

  7. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  8. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  9. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

随机推荐

  1. SpringBoot 2.x 开发案例之前后端分离鉴权

    前言 阅读本文需要一定的前后端开发基础,前后端分离已成为互联网项目开发的业界标准使用方式,通过Nginx代理+Tomcat的方式有效的进行解耦,并且前后端分离会为以后的大型分布式架构.弹性计算架构.微 ...

  2. GO中的逃逸分析

    1.什么是逃逸分析 以前写c/c++代码时,为了提高效率,常常将pass-by-value(传值)“升级”成pass-by-reference,企图避免构造函数的运行,并且直接返回一个指针. 那么这里 ...

  3. Linux c++ vim环境搭建系列(1)——Ubuntu18.04.4编译安装vim8.2

    1. vim源码编译安装 参考网址: https://github.com/ycm-core/YouCompleteMe/wiki/Building-Vim-from-source 安装各类依赖库 s ...

  4. (转) Windows Mobile和Windows CE的区别

    转发自 http://blog.sina.com.cn/s/blog_6250bbe60100tsf3.html WinCE Windows CE 是一个可定制的操作系统: Windows Mobil ...

  5. VXLAN 基础教程:在 Linux 上配置 VXLAN 网络

    上篇文章结尾提到 Linux 是支持 VXLAN 的,我们可以使用 Linux 搭建基于 VXLAN 的 overlay 网络,以此来加深对 VXLAN 的理解,毕竟光说不练假把式. 1. 点对点的 ...

  6. PHP xml 外部实体注入漏洞学习

    XML与xxe注入基础知识 1.XMl定义 XML由3个部分构成,它们分别是:文档类型定义(Document Type Definition,DTD),即XML的布局语言:可扩展的样式语言(Exten ...

  7. NGINX 类漏洞 整理记录

    简单介绍NGINX: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行. 其特点是占有内存少,并发能力强,nginx的并 ...

  8. python数据结构之栈的构建

    class Stack(object): def __init__(self): self.stack=[] def pop(self): return self.stack.pop() def pu ...

  9. [leetcode] 并查集(Ⅰ)

    预备知识 并查集 (Union Set) 一种常见的应用是计算一个图中连通分量的个数.比如: a e / \ | b c f | | d g 上图的连通分量的个数为 2 . 并查集的主要思想是在每个连 ...

  10. thinkphp5 -- _initialize()初始化控制器

    public function _initialize() { parent::_initialize(); } public function __construct() { $; parent:: ...