#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. Silverlight Popup Bubble

    控件下载地址: http://www.pudn.com/downloads217/sourcecode/others/detail1023372.html silverlight工程引入Liquid. ...

  2. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. Centos7 安装 Nginx

    Nginx有很多版本的,下面我给个链接http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/ 下载对应当前系统版本的nginx包(packag ...

  4. jsp页面指令

    JSP中共有三个指令: (1)page: 用于定义JSP文件中的全局属性 (2)include: 用于在JSP页面中包含另外一个文件的内容 (3)taglib: 此指令能够让用户自定义新的标签 第三个 ...

  5. 对while((pid = waitpid(-1, &stat, WNOHANG)) > 0)不懂的地方,现在懂了

    while((pid = waitpid(-1, &stat, WNOHANG)) > 0) 需要写到信号处理函数中,假如有10个子进程 只要父进程能够收到最后一个信号,就能把前面丢失的 ...

  6. Oracle中游标返回多条数据的情况

    DECLARE -- 定义类型. TYPE test_type IS TABLE OF test_main%ROWTYPE; test_data test_type; -- 定义游标. CURSOR ...

  7. [MacOS] xcrun: error: active developer path ("/Volumes/Xcode/Xcode6-Beta.app/Contents/Developer") does not exist, use xcode-select to change

    When using MacOS with xcode6-beta, i always meet these error: xcrun: error: active developer path (& ...

  8. C# 时间现实问题(12小时制与24小时制)

    最近在修改项目中遇到时间问题,12小时制与24小时制的问题,想再次跟各位同仁提个醒. yyyy-MM-dd HH:mm:ss------大写的HH为24小时制 yyyy-MM-dd hh:mm:ss- ...

  9. hadoop文件系统FileSystem详解 转自http://hi.baidu.com/270460591/item/0efacd8accb7a1d7ef083d05

    Hadoop文件系统 基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件. Java抽象类org.apache.hadoop.fs.FileSystem定义了 ...

  10. [置顶] Jquery学习总结(二) jquery选择器详解

    1.基本选择器 l ID 根据元素ID选择 l Elementname 根据元素名称选择 l Classname 根据元素css类名选择 举例: <input type=”text” id=”I ...