动态规划之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( ...
随机推荐
- AWR--导出AWR数据
SQL> create or replace directory expdp_dir as '/home/oracle/dump'; Directory created. SQL> @$O ...
- web app 自适应 弹性布局之rem
关于rem,主要参考文档 1.腾讯ISUX (http://isux.tencent.com/web-app-rem.html) 2.http://www.w3cplus.com/css3/defin ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- Swift实战-小QQ(第2章):QQ侧滑菜单
QQ侧滑实现架构:需要建立以下几个ViewController:1.XQBaseViewController 2.LeftViewController3.RightViewController4.Co ...
- Swift游戏实战-跑酷熊猫 13 二段跳的实现
这节内容我们来实现熊猫的二段跳. 要点: 二段跳的逻辑: 逻辑一,第一次点击屏幕,status就会变成jump. 逻辑二,第二次点击屏幕,status就会变成jump2. 逻辑三,当status变成j ...
- 源码搭建LNMP
源码安装LNMP 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...
- CCF真题之字符串匹配
201409-3 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一 ...
- 在GitHub上建立个人主页的方法(转载)
GitHub就不需要介绍了,不清楚可以百度一下.只说目前GitHub是最火的开源程序托管集中地了,连PHP的源码都在GitHub上面托管了(https://github.com/php ). GitH ...
- paper 63 :函数比较:imfilter与fspecial
功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...
- jQuery讲解
在讲解jQuery时,要和JavaScript进行对比的讲解,易于理解 JavaScript部分 <title>jquery讲解使用</title> <script sr ...