链接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. ERP反馈信息管理(十九)

    前台显示的界面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Custo ...

  2. Team Foundation API - 编程访问 WorkItem

    Team Foundation Server (TFS)工具的亮点之一是管理日常工作项, 工作项如Bug, Task,Task Case等. 使用TFS API编程访问TFS服务器中的工作项, 步骤如 ...

  3. iOS-NSThread使用

    NSThread: 优点:NSThread 比其他两个轻量级(Cocoa NSOperation.GCD) 缺点:需要自己管理线程的生命周期,线程同步.线程同步对数据的加锁会有一定的系统开销 Coco ...

  4. 转:Enterprise Library 4.0缓存应用程序块

    英文原文:http://msdn.microsoft.com/zh-cn/library/cc511588(en-us).aspx Enterprise Library 缓存应用程序块允许开发人员在应 ...

  5. mysql 获得当前月1号的日期 和 0点日期方法

    day)) 当月0点时间:2015-12-01 00:00:00 day)) 当月1号时间:2015-12-01 10:45:22 day) 2015-12-01 <!-- 查询上月债权额度 - ...

  6. CSS 实现:图片+文字的布局(综合)

    ☊[实现要求]:图片+文字+居中 √[实现]: ① img + 文字 <div class="demo2-1"> <img src="" al ...

  7. ZMMR106-批量更新PO交货日期

    ************************************************************************ Title : ZMMR106 ** Applicat ...

  8. leetcode 111 Minimum Depth of Binary Tree ----- java

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. Android 中解析 JSON

    有什么不懂的可以去官网去看看:www.json.org 在google android中也有关于解析JSON的类库:JsonReader,但是只能在3.0以后的版本中才可以用,在这里我们用google ...

  10. Mysql存储日期类型用int、timestamp还是datetime?

    通常存储时间用datetime类型,现在很多系统也用int存储时间,它们有什么区别?个人更喜欢使用int这样对于日期计算时比较好哦,下面我们一起来看到底那种会好些. int ().4个字节存储,INT ...