Algorithm --> 最长公共子序列(LCS)
<1> 枚举法
这种方法是最简单,也是最容易想到的,当然时间复杂度也是龟速的,我们可以分析一下,刚才也说过了cnblogs的子序列个数有27个 ,延伸一下:一个长度为N的字符串,其子序列有2N个,每个子序列要在第二个长度为N的字符串中去匹配,匹配一次需要O(N)的时间,总共也就是O(N*2N),可以看出,时间复杂度为指数级,恐怖的令人窒息。
<2> 动态规划
最长公共子序列的结构有如下表示:

代码:
//只能打印一个最长公共子序列
#include <iostream>
using namespace std; const int X = , Y = ; //串的最大长度
char result[X+]; //用于保存结果
int count=; //用于保存公共最长公共子串的个数 /*功能:计算最优值
*参数:
* x:字符串x
* y:字符串y
* b:标志数组
* xlen:字符串x的长度
* ylen:字符串y的长度
*返回值:最长公共子序列的长度
*
*/
int Lcs_Length(string x, string y, int b[][Y+],int xlen,int ylen)
{
int i = ;
int j = ; int c[X+][Y+];
for (i = ; i<=xlen; i++)
{
c[i][]=;
}
for (i = ; i <= ylen; i++ )
{
c[][i]=;
}
for (i = ; i <= xlen; i++)
{
for (j = ; j <= ylen; 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 if(c[i-][j] <= c[i][j-])
{
c[i][j] = c[i][j-];
b[i][j] = ;
}
}
}
return c[xlen][ylen];
} void Display_Lcs(int i, int j, string x, int b[][Y+],int current_Len)
{
if (i == || j==)
{
return;
}
if(b[i][j]== )
{
current_Len--;
result[current_Len]=x[i- ];
Display_Lcs(i-, j-, x, b, current_Len);
}
else if(b[i][j] == )
{
Display_Lcs(i-, j, x, b, current_Len);
}
else if(b[i][j]==)
{
Display_Lcs(i, j-, x, b, current_Len);
}
else
{
Display_Lcs(i-,j,x,b, current_Len);
}
} int main(int argc, char* argv[])
{
string x = "ABCBDAB";
string y = "BDCABA";
int xlen = x.length();
int ylen = y.length(); int b[X + ][Y + ]; int lcs_max_len = Lcs_Length( x, y, b, xlen,ylen );
cout << lcs_max_len << endl; Display_Lcs( xlen, ylen, x, b, lcs_max_len ); //打印结果如下所示
for(int i = ; i < lcs_max_len; i++)
{
cout << result[i];
}
cout << endl;
return ;
}
运行结果如下:
BDAB
由于有时并不是只有一个最长公共子序列,所以,对上面的代码进行改进,增加一个数组保存结果等....代码如下所示。
//求取所有的最长公共子序列
#include <iostream>
using namespace std; const int X = , Y = ; //串的最大长度
char result[X+]; //用于保存结果
int cnt = ; //用于保存公共最长公共子串的个数 /*功能:计算最优值
*参数:
* x:字符串x
* y:字符串y
* b:标志数组
* xlen:字符串x的长度
* ylen:字符串y的长度
*返回值:最长公共子序列的长度
*
*/
int Lcs_Length(string x, string y, int b[][Y+],int xlen,int ylen)
{
int i = ;
int j = ; int c[X+][Y+];
for (i = ; i<=xlen; i++)
{
c[i][]=;
}
for (i = ; i <= ylen; i++ )
{
c[][i]=;
}
for (i = ; i <= xlen; i++)
{ for (j = ; j <= ylen; 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 if(c[i-][j] < c[i][j-])
{
c[i][j] = c[i][j-];
b[i][j] = ;
}
else
{
c[i][j] = c[i][j-]; //或者c[i][j]=c[i-1][j];
b[i][j] = ;
}
}
}
return c[xlen][ylen];
} /*功能:计算最长公共子序列
*参数:
* xlen:字符串x的长度
* ylen:字符串y的长度
* x :字符串x
* b:标志数组
* current_len:当前长度
* lcs_max_len:最长公共子序列长度
*
*/
void Display_Lcs(int i, int j, string x, int b[][Y+],int current_len,int lcs_max_len)
{
if (i == || j==)
{
for(int s=; s < lcs_max_len; s++)
{
cout << result[s];
}
cout<<endl;
cnt++;
return;
}
if(b[i][j]== )
{
current_len--;
result[current_len]=x[i- ];
Display_Lcs(i-, j-, x, b,current_len,lcs_max_len);
}
else if(b[i][j] == )
{
Display_Lcs(i-, j, x, b,current_len,lcs_max_len);
}
else if(b[i][j]==)
{
Display_Lcs(i, j-, x, b,current_len,lcs_max_len);
}
else
{
Display_Lcs(i,j-,x,b,current_len,lcs_max_len);
Display_Lcs(i-,j,x,b,current_len,lcs_max_len);
}
} int main(int argc, char* argv[])
{
string x = "ABCBDAB";
string y = "BDCABA";
int xlen = x.length();
int ylen = y.length(); int b[X + ][Y + ]; int lcs_max_len = Lcs_Length( x, y, b, xlen,ylen );
cout << lcs_max_len << endl; Display_Lcs( xlen, ylen, x, b, lcs_max_len, lcs_max_len );
cout << "共有:" << cnt << "种"; return ;
}
运行结果如下:
BDAB
BCAB
BCBA
共有: 种
/****************************************
update
****************************************/
示例:
#include <iostream>
using namespace std;
#define X_LEN 7
#define Y_LEN 6
#define EQUAL 0
#define UP 1
#define LEVEL 2
void lcs_length(char* X,char* Y,int c[X_LEN+][Y_LEN+],int b[X_LEN+][Y_LEN+]);
void print_lcs(int b[X_LEN+][Y_LEN+],char *X,int i,int j); int main()
{
char X[X_LEN+] = {' ','A','B','C','B','D','A','B'};
char Y[Y_LEN+] = {' ','B','D','C','A','B','A'};
int c[X_LEN+][Y_LEN+]={};
int b[X_LEN+][Y_LEN+] = {};
int i,j;
lcs_length(X,Y,c,b);
for(i=;i<=X_LEN;i++)
{
for(j=;j<=Y_LEN;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
cout<<"The length of LCS is: "<<c[X_LEN][Y_LEN]<<endl;
cout<<"The longest common subsequence between X and y is: "<<endl;
print_lcs(b,X,X_LEN,Y_LEN);
return ;
}
//采用动态规划方法自底向上的进行计算,寻找最优解
void lcs_length(char* X,char* Y,int c[X_LEN+][Y_LEN+],int b[X_LEN+][Y_LEN+])
{
int i,j;
//设置边界条件,即i=0或者j=0
for(i=;i<X_LEN;i++)
c[i][] = ;
for(j=;j<Y_LEN;j++)
c[][j] = ;
for(i=;i<=X_LEN;i++)
for(j=;j<=Y_LEN;j++)
{
if(X[i] == Y[j]) //满足递归公式第二条
{
c[i][j] = c[i-][j-]+;
b[i][j] = EQUAL ;
}
else if(c[i-][j] >= c[i][j-]) //递归公式第三条
{
c[i][j] = c[i-][j];
b[i][j] = UP;
}
else
{
c[i][j] = c[i][j-];
b[i][j] = LEVEL;
}
}
}
void print_lcs(int b[X_LEN+][Y_LEN+],char *X,int i,int j)
{
if(i== || j==)
return;
if(b[i][j] == EQUAL)
{
print_lcs(b,X,i-,j-);
cout<<X[i]<<" ";
}
else
if(b[i][j] == UP)
print_lcs(b,X,i-,j);
else
print_lcs(b,X,i,j-);
}
结果:
Algorithm --> 最长公共子序列(LCS)的更多相关文章
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51nod 1006:最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 每日一题-——最长公共子序列(LCS)与最长公共子串
最长公共子序列(LCS) 思路: 代码: def LCS(string1,string2): len1 = len(string1) len2 = len(string2) res = [[0 for ...
随机推荐
- eclipse生成【带有外部jar包】的java可执行jar包
之前有写过一篇使用eclipse生成java可执行jar包,但是最近的一次使用中无论如何都不成功,当双击执行打成的jar时,弹出如下错误: could not find the main class: ...
- sublime Xdebug 配置
Sublime Text 配置x-debug 配置php 的x-debug 拓展 下载地址 :http://www.xdebug.org/download.php 放到php ext的目录下 然后使用 ...
- 深究ASP.NET Session
Session 本质 & 访问方法 Session 本质 是 HttpSessionState 类 link Session 访问方法 HttpContext.Session Page.Ses ...
- 关于spring的aop拦截的问题 protected方法代理问题
看到一篇很好的Spring aop 拦截方法的问题, 原文地址. 问题 貌似不能拦截私有方法? 试了很多次,都失败了,是不是不行啊? 我想了一下,因为aop底层是代理, jdk是代理接口,私有方法必 ...
- Spring中的IOC和AOP是什么含义,他们在项目中起到什么作用,并举例说明?
IOC:控制反转,是一种设计模式.一层哈尼是控制权的转移:由传统的在程序中控制并依赖转移到容器赖控制:第二是依赖注入:将相互以来的对象分离,在Spring配置文件中描述他们的依赖关系.他们的依赖关系只 ...
- I/HwPointEventFilter: do not support AFT because of no config
I/HwPointEventFilter: do not support AFT because of no config 这是华为对系统做了修改,默认不打印日志,要改配置 在拨号界面输入:以下进入工 ...
- Bzoj1901 Dynamic Ranking
动态区间第k小 离散化后 那么每个点开一棵线段树(主席树)再套一个树状数组在外面 每次询问区间内的树的个数时 相当于进行了一次树状数组求区间和的操作,只是是把树状数组那个点看做主席树,对log棵主席树 ...
- [BZOJ2761] [JLOI2011] 不重复数字 (set)
Description 给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 ...
- C# QQ邮箱注册,以及数秒
一. 这是前台需要的东西 <asp:TextBox ID="Textemail" runat="server" CssClass="nonein ...
- python之hasattr()、 getattr() 、setattr() 函数
这三个方法可以实现反射和内省机制,在实际项目中很常用,功能也很强大. [转]http://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, na ...