链接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)的更多相关文章

  1. LCS最长公共子序列(最优线性时间O(n))

    这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...

  2. 动态规划经典——最长公共子序列问题 (LCS)和最长公共子串问题

    一.最长公共子序列问题(LCS问题) 给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子序列,并返回其长度.例如: A = "HelloWorld"    B = & ...

  3. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  4. 最长公共子序列/子串 LCS(模板)

    首先区分子序列和子串,序列不要求连续性(连续和不连续都可以),但子串一定是连续的 1.最长公共子序列 1.最长公共子序列问题有最优子结构,这个问题可以分解称为更小的问题 2.同时,子问题的解释可以被重 ...

  5. 最长公共子序列(LCS)和最长递增子序列(LIS)的求解

    一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...

  6. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  7. LIS(最长的序列)和LCS(最长公共子)总结

    LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...

  8. 程序员的算法课(6)-最长公共子序列(LCS)

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  9. 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

    最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

  10. 最大公共字串LCS问题(阿里巴巴)

    给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...

随机推荐

  1. ecpilise引入Maven项目目录不正常,无JRE,无Maven Dependencies

    原因是我的eclipse默认open perspective是java ee,改成java就恢复正常了.

  2. Palindrome_滚动数组&&DP

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  3. iOS 难题解决日志------2层控制器 上面的控制器显示透明

     f ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) { nextVC.modalPresentationStyle=U ...

  4. C#: .net序列化及反序列化 [XmlElement(“节点名称”)]

    .net序列化及反序列化 序列化是指一个对象的实例可以被保存,保存成一个二进制串,当然,一旦被保存成二进制串,那么也可以保存成文本串了.比如,一个计数器,数值为2,我们可以用字符串“2”表示.如果有个 ...

  5. jQuery ajax传多个参数

    ajax可以传送一个或多个参数到后台php中 <script> $(function(){ $("#sub_btn").click(function(){ var em ...

  6. pscp详解

    pscp详解 在linux中,我们常用scp命令传输文件: 如以下实例,我们想把当前服务器文件abc.sql传输到192.168.1.1服务器上,我们可以执行以下命令: scp /home/perso ...

  7. 怎样将某一类型标识为适合绑定到 System.Web.UI.WebControls.ObjectDataSource 对象的对象

    1.页面的代码如下: body> <form id="form1" runat="server"> <div> </div& ...

  8. 创建XML文档结构

    static void CreateXML(string outputPath) { XmlDocument _xmlDoc = new XmlDocument(); string _xmlNode ...

  9. 动画(Animation) 、 高级动画(Core Animation)

    1 演示UIImage制作的动画 1.1 问题 UIImage动画是IOS提供的最基本的动画,通常用于制作一些小型的动画,本案例使用UIImage制作一个小狗跑动的动画,如图-1所示: 图-1 1.2 ...

  10. iOS开发环境C语言基础 运算符和表达式

    1 年龄判断程序 1.1 问题 本案例需要使用交互的方式判断年龄的范围:用户从控制台输入一个年龄,由程序判断该年龄是否在18~50岁之间.程序交互过程如图-1所示: 图-1 1.2 步骤 实现此案例需 ...