最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220
链接2:http://blog.csdn.net/x_xiaoge/article/details/7376217
链接3:http://www.cnblogs.com/huangxincheng/archive/2012/11/11/2764625.html
LCS
1、动态规划法:见链接1、3
int LCS( string leftString, string rightString )
{ int lenLeft = leftString.length();
int lenRight = rightString.length();
int **martix;
martix = new int *[lenLeft+1]; for (int i=0; i<=lenLeft; i++)
{
martix[i] = new int [lenRight+1];
} for (int i = 0; i <= lenLeft; i++)
martix[i][0] = 0; for (int j = 0; j <= lenRight; j++)
martix[0][j] = 0; for (int i = 1; i <= lenLeft; i++)
{
for (int j = 1; j <= lenRight; j++)
{
if (leftString[i-1] == rightString[j-1])
{
martix[i][j] = martix[i-1][j-1] + 1;
}
else
{
if (martix[i-1][j] >= martix[i][j-1])
martix[i][j] = martix[i-1][j];
else
martix[i][j] = martix[i][j-1];
}
}
} return martix[lenLeft][lenRight];
}
LCCS:
1、递归算法如下:
string GetLongestString(string strTmp1, string strTmp2,string strTmp3)
{
int len1 = strTmp1.length();
int len2 = strTmp2.length();
int len3 = strTmp3.length();
if (len1 > len2)
{
if (len1 > len3)
{
return strTmp1;
}
}
else if (len2 > len3)
{
return strTmp2;
}
return strTmp3;
} void LCCS(const std::string& str1, const std::string& str2,std::string& lccs)
{
if(str1.length() == || str2.length() == )
return; if(str1[] == str2[])
{
lccs += str1[];
LCCS(str1.substr(), str2.substr(), lccs);
}
else
{
std::string strTmp1,strTmp2,strTmp3; LCCS(str1.substr(), str2, strTmp1);
LCCS(str1, str2.substr(), strTmp2);
LCCS(str1.substr(), str2.substr(), strTmp3);
std::string strLongest = GetLongestString(strTmp1, strTmp2, strTmp3);
if(lccs.length() < strLongest.length())
lccs = strLongest;
}
}
最长公共字串(LCS)最长连续公共字串(LCCS)的更多相关文章
- LCS最长公共子序列(最优线性时间O(n))
这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...
- 动态规划经典——最长公共子序列问题 (LCS)和最长公共子串问题
一.最长公共子序列问题(LCS问题) 给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子序列,并返回其长度.例如: A = "HelloWorld" B = & ...
- SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)
http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...
- 最长公共子序列/子串 LCS(模板)
首先区分子序列和子串,序列不要求连续性(连续和不连续都可以),但子串一定是连续的 1.最长公共子序列 1.最长公共子序列问题有最优子结构,这个问题可以分解称为更小的问题 2.同时,子问题的解释可以被重 ...
- 最长公共子序列(LCS)和最长递增子序列(LIS)的求解
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- LIS(最长的序列)和LCS(最长公共子)总结
LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...
- 程序员的算法课(6)-最长公共子序列(LCS)
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)
最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...
- 最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...
随机推荐
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- VS.net中快捷键收缩和展开代码段
i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M 折叠或展开当前方法 iv. Ctrl-M-L展开所 ...
- 查询语句中select from where group by having order by的执行顺序
查询语句中select from where group by having order by的执行顺序 1.查询中用到的关键词主要包含六个,并且他们的顺序依次为 select--from--w ...
- 【avalon】data
if (root.dataset) { avalon.fn.data = function (name, val) { name = name && camelize(name) va ...
- OpenFlow Switch学习笔记(六)——Instructions和Actions
本文主要重点讨论OpenFlow Switch规范的指令集,它们深刻影响着数据包在Switch中的处理行为,下面开始从以下几个部分谈起. 1.Instructions 每一个Flow Entry里都包 ...
- smartmontools的安装使用和实现对磁盘的Nagios监控
安装 首先从sourceforge下载最新的安装版本. 解压编译 $ tar -zxvf smartmontools-6.4.tar.gz $ cd smartmontools-6.4 $ ./con ...
- 基于Spring框架的Web应用开发笔记 - Outline
Motivation 最近的工作涉及Web框架搭建,在了解公司原有采用框架基础上对Web开发技术栈做了一次升级,在次做记录. Audience J2EE Web Application Develop ...
- crontab 安装 和一些 简单的命令
crontab命 令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供 之后读取和执行.通常,crontab ...
- jQuery弹出层始终垂直居中相对于屏幕或当前窗口
把弹出层的位置设为fixed,设置top:50%,然后获取当前元素的整体的高度height,用获取的高度height/2,设置margin-top:-height/2.即可把当前的弹出层始终垂直居中于 ...
- 设置只为View加一条边框,子视图大小超出父视图大小,边框在子视图下边显示
#import "ViewController.h" @interface ViewController () @property (strong,nonatomic) UIVie ...