动态规划之LCS(最大公共子序列)
#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(最大公共子序列)的更多相关文章
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...
- python3 lcs 最大公共子序列
抛出问题: 假定字符串 s1 = 'BDCABA', s2 = 'ABCBDAB',求s1和s2的最大公共子序列. 问题分析: 我们想要求出s1和s2的最大公共子序列,我们可以用c(i,j)表示s1( ...
- LCS最大公共子序列问题
在生物应用中,经常需要比较两个(或多个)不同生物体的DNA, 例如:某种生物的DNA可能为S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, 另一种生物的DNA可能为S2=GTCGTT ...
- LCS最大公共子序列【转载】
在两个字符串中,有些字符会一样,可以形成的子序列也有可能相等,因此,长度最长的相等子序列便是两者间的最长公共字序列,其长度可以使用动态规划来求. 以s1={1,3,4,5,6,7,7,8},s2={3 ...
- Advanced Fruits (最大公共子序列的路径打印)
The company "21st Century Fruits" has specialized in creating new sorts of fruits by trans ...
- hdu 1243 反恐训练营(dp 最大公共子序列变形)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1243 d[i][j] 代表第i 个字符与第 j 个字符的最大的得分.,, 最大公共子序列变形 #inclu ...
- nyoj 37-回文字符串(reverse, 动态规划, lcs)
37-回文字符串 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:17 题目描述: 所谓回文字符串,就是一个字符串,从左到右读和从 ...
- spoj Longest Common Substring (多串求最大公共子序列)
题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...
- (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离
斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...
随机推荐
- max-min fairness 最大最小公平算法
我们经常面临给一组用户划分稀有资源的问题,他们都享有等价的权利来获取资源,但是其中一些用户实际上只需要比其他用户少的资源.那么我们如何来分配资源呢?一种在实际中广泛使用的分享技术称作“最大最小公平分享 ...
- vi/vim 键盘图 & 替换
在VIM中进行文本替换: 1. 替换当前行中的内容: :s/from/to/ (s即substitude) :s/from/to/ : 将当前行中的第一个f ...
- prezi破解教程
http://www.joenchen.com/archives/998 http://www.joenchen.com/archives/945 Prezi Desktop 4.7.5免注册无时间限 ...
- node.js http.get 和http.post 数据
http.get('http://codestudy.sinaapp.com', function (response) {}); node.js http post 样例: var reqData= ...
- RelativeLayout相对布局属性
RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...
- cocos2d-x游戏开发之烟花粒子效果
//散烟花及“太”“棒”“了”效果 void mygame::playfire() { sprite *tai = sprite::create("tai.png"); tai-& ...
- 管理科学与工程 国内核心期刊 国外a刊及SCI
国内: 管理科学与工程: 管理科学学报 A+ (匿名审稿,绝对牛刊,不比一般的SCi期刊的质量差) 系统工程理论与实践 A (实名审稿,关系稿很多,尤其是挂编委的文章很多,但质量尚可)系统工程 ...
- ThinkPHP讲解(七)——修改删除
修改数据 方式一:数组方式,直接将数据库里需要修改的内容进行修改 function Update() { //1.数组方式 $model=D("Info"); $attr=arra ...
- getResource().getPath()返回的路径空格变成了 %20
this.getClass().getResource(“/”).getPath()使用者方法查看文件在服务器上的地址,但是地址中的空格会被转化为%20. 解决办法1: URI uri = new U ...
- Objective-C语言控制语句
• 分支语句• 循环语句• 跳转语句 Objective-C中的控制语句有以下几类:• 分支语句:if-else, switch• 循环语句:while, do-while, for• 与程序转移有关 ...