最长公共字串(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 ...
随机推荐
- iptables_forward
FORWARD 好了,我们接着往下走,这个包已经过了两个PREROUTING链了,这个时候,出现了一个分支转折的地方,也就是图中下方的那个菱形(FORWARD),转发!这里有一个对目的地址的判断(这里 ...
- 结构体用于map,set时要重载运算符<
#include<iostream> #include<set> using namespace std; struct P { int entry; int time; bo ...
- 收藏:关于UseSubmitBehavior和OnClientClick同时使用,导致无法触发后台事件的问题
经常会有正样的需求,在用户做一个不易恢复并且容易误操作的动作时需要给用户以提示,用户确认后继续执行动作. 简单的解决方案是:前台用OnClientClick事件中使用Confirm给用弹出确认提示框, ...
- CSS 实现:图片+文字的布局(综合)
☊[实现要求]:图片+文字+居中 √[实现]: ① img + 文字 <div class="demo2-1"> <img src="" al ...
- java设定窗口步长,依次统计窗口内数值总和
import java.util.Arrays; public class test2 { public static void main(String[] args) { int winSize = ...
- Quantum & r2q
Quantum & r2q Let's assume we have 2 classes with the same parent : Parent : ceil = rate = 100 c ...
- R 学习1
首先安装吧 http://cran.rstudio.com/bin/windows/base/R-3.2.1-win.exe 里面既有32位又有64. R有很多包,如果有的包本地没有,来这里搜 htt ...
- 如何用cufflinks 拼出一个理想的注释文件
后记: cufflinks安装: 下载安装包, 不要下载source code ,直接下载binary. Source code Linux x86_64 binary http://cu ...
- 对UICollectionView的学习
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...
- Codeforces Round #366 (Div. 2) C 模拟queue
C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...