#include <stdio.h>
#include <string.h> int b[50][50];
int c[50][50];
int length = 0; void lcs(char *x, int m, char *y, int n)
{
int i;
int j; for(i = 1; i <= m; i++)
c[i][0] = 0; for(i = 1; i <= n; i++)
c[0][i] = 0; for(i = 1; i <= m; i++)
{
for(j = 1; j <= n; j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1;
}
else if(c[i-1][j] > c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 2;
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = 3;
}
}
}
} void show(int i, int j, char *x)
{
if(i == 0 || j ==0)
return; if(b[i][j] == 1)
{
show(i-1, j-1, x);
length++;
printf("%c", x[i-1]);
}
else if(b[i][j] == 2)
{
show(i-1, j, x);
}
else
{
show(i, j-1, x);
}
} int main()
{
char *x = "xyzrfdt";
char *y = "xzhrgfiut"; //xzrft int m = strlen(x);
int n = strlen(y);
lcs(x,m,y,n); printf("The string is: \n");
show(m, n, x);
}
 #include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
string str1 = "ABCBDAB";
string str2 = "BDCABA"; int x_len = str1.length();
int y_len = str2.length(); int arr[][] = {{,}}; int i = ;
int j = ; for(i = ; i <= x_len; i++)
{
for(j = ; j <= y_len; j++)
{
if(str1[i - ] == str2[j - ])
{
arr[i][j] = arr[i - ][j - ] + ;
}
else
{ if(arr[i][j - ] >= arr[i - ][j])
{
arr[i][j] = arr[i][j - ];
}
else
{
arr[i][j] = arr[i -][j];
}
} }
}
for(i = ; i <= x_len; i++)
{
for( j = ; j <= y_len; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
for(i = x_len, j = y_len; i >= && j >= ;)
{
if(str1[i - ] == str2[j - ])
{
cout << str1[i - ] << " ";//倒序打印的
i--;
j--;
}
else
{
// if(arr[i][j -1] >= arr[i - 1][j])//打印:B A D B
if(arr[i][j -] > arr[i - ][j]) //打印:A B C B
{
j--;
}
else
{
i--;
}
}
}
cout << endl;
return ;
}

C++

运行结果:

 

动态规划之LCS(最大公共子序列)的更多相关文章

  1. Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)

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

  2. python3 lcs 最大公共子序列

    抛出问题: 假定字符串 s1 = 'BDCABA', s2 = 'ABCBDAB',求s1和s2的最大公共子序列. 问题分析: 我们想要求出s1和s2的最大公共子序列,我们可以用c(i,j)表示s1( ...

  3. LCS最大公共子序列问题

    在生物应用中,经常需要比较两个(或多个)不同生物体的DNA, 例如:某种生物的DNA可能为S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, 另一种生物的DNA可能为S2=GTCGTT ...

  4. LCS最大公共子序列【转载】

    在两个字符串中,有些字符会一样,可以形成的子序列也有可能相等,因此,长度最长的相等子序列便是两者间的最长公共字序列,其长度可以使用动态规划来求. 以s1={1,3,4,5,6,7,7,8},s2={3 ...

  5. Advanced Fruits (最大公共子序列的路径打印)

    The company "21st Century Fruits" has specialized in creating new sorts of fruits by trans ...

  6. hdu 1243 反恐训练营(dp 最大公共子序列变形)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1243 d[i][j] 代表第i 个字符与第 j 个字符的最大的得分.,, 最大公共子序列变形 #inclu ...

  7. nyoj 37-回文字符串(reverse, 动态规划, lcs)

    37-回文字符串 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:17 题目描述: 所谓回文字符串,就是一个字符串,从左到右读和从 ...

  8. spoj Longest Common Substring (多串求最大公共子序列)

    题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...

  9. (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离

    斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...

随机推荐

  1. Radius session

    1,EAP 中继 client start, NAS require identity, client sent username, NAS sent username to sever, serve ...

  2. 头部固定下面滑动&&获取手机屏高

    height(){ //获取屏高 let phone_height = document.documentElement.clientHeight; let group = this.refs.sea ...

  3. linux:磁碟与档案系统管理

    档案系统特性:为什么磁碟分割完需要格式化(format)才能使用吗? 答:因为每种作业系统所设定的档案属性和权限并不相同,为了存放这些档案所需的资料(所以需要格式化成作业系统能够利用的档案系统格式fi ...

  4. HTML语言的一些元素(五)

    一.table表格语法与结构 <table> <tr> <td> </td> <td> </td> <td> < ...

  5. Swift游戏实战-跑酷熊猫 00 游戏预览

    这个系列我们将要一起来做一个这样的游戏

  6. Servlet 基础认识 使用

    参考书<JSP Web 开发案例教程> 在jsp项目中的src文件下面创建HelloServletTest.java,代码如下,注意src的包名,我的包名是zhbit.com packag ...

  7. 16---Net基础加强

    更新中,敬请期待............ Xml介绍 xml读写练习 xml练习1 xml练习2

  8. 06---Net基础加强

    字符串特性-池-不可变性 class Program { static void Main(string[] args) { #region 判断两个对象是否是同一个对象 //Person p1 = ...

  9. cvLoadImage函数解析 cvLoadImageM()函数

    1.函数原型:IplImage* cvLoadImage( const char* filename, int flags=CV_LOAD_IMAGE_COLOR ); filename :要被读入的 ...

  10. OpenStack 的防火墙规则流程

    Contents [hide] 1 发现的问题 2 解决过程 3 删除临时错误数据 4 其实前面的解决办法是错的 发现的问题 3台虚拟机在同一宿主机,防火墙配置都一样,但是他们的网络表现不一致,有的能 ...