LCS
/*
*LCS问题
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
//cout << "hello, world" << endl;
string str1 = "abcbdab";
string str2 = "bdcaba";
int x_len = str1.length();
int y_len = str2.length();
int i, j;
int arr[50][50] = {0};
cout << str1 << endl << str2 << endl;
for(i = 1; i <= x_len; i++){
for(j = 1; j <= y_len; j++){
if(str1[i-1] == str2[j-1])
{
arr[i][j] = arr[i-1][j-1] + 1;
}
else
{
if(arr[i][j-1] >= arr[i-1][j])
arr[i][j] = arr[i][j-1];
else
arr[i][j] = arr[i-1][j];
}
}
}
//
for(i = 0 ; i <= x_len; i++)
{
for(j = 0; j <= y_len; j++)
cout << arr[i][j] << " ";
cout << endl;
}
//
string ret;
for(i = x_len,j = y_len; i >= 1 && j >= 1; )
{
if(str1[i-1] == str2[j-1]){
//cout << str1[i-1] << " ";
ret += str1[i-1];
i--;
j--;
}
else{
if(arr[i][j-1] > arr[i-1][j])
{
j--;
}
else
i--;
}
}
reverse(ret.begin(), ret.end());
cout << ret;
cout << endl;
return 0;
}
LCS的更多相关文章
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- Hackerrank11 LCS Returns 枚举+LCS
Given two strings, a and , b find and print the total number of ways to insert a character at any p ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
- 最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
随机推荐
- 理解innodb buffer pool
今天组里有个同事说可以查看innodb buffer pool每个表和索引占的大小,为此我搜了下,还真有方法,记录下. innodb buffer pool有几个目的: 缓存数据--众所周知,这个占了 ...
- [已解决] git 重命名文件夹
git mv oldfolder newfolder 原文地址:http://www.cnblogs.com/gifisan/p/5980608.html
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
转自http://blog.csdn.net/testcs_dn/article/details/37834063 c#DataGridView数据绑定示例 格式化单元格的内容 在使用DataGrid ...
- 关于mysql 查询内容不区分大小问题
问题描述: select * from users where user_name ='user_01' 跟 select * from users where user_name ='uSer_01 ...
- js_保留关键字
网页可以被我们分为三个大的部分:结构,表现,形式而js就是专对于表现的,js是一门编程的解释性脚本语言,和其他的语言相同js也有自己的保留的关键字,下面我们来看看js保留的关键字吧!js一共有56个关 ...
- Centos7永久修改主机名
最近在编写centos6.x的启动脚本,考虑到以后系统会升到7.x,故想让脚本兼容7.x,还是有一些和6版本不一样的地址,修改主机名比较常用,特此记录一下 1.命令行修改: hostnamectl s ...
- ubuntu 14.04 vsftpd安装问题
sudo apt-get install vsftpd; 打开允许访问用户名 local_enable=YES write_enable=YES chroot_list_file=/etc/vsftp ...
- 第三天的学习知识HTML5常用的重要单词
a: a:猫 address:地址 alt:替用(一般是图片显示不出的提示) b: b:粗体 br:换行 background:背景 border:边框 ...
- python 中的 try/except/else/finally语句
1.python中try/except/else/finally正常的语句是这样的: try: normal excute block except A: Except A handle except ...
- in_array,array_search的使用
写一个数组里面有小写a-z 大写A-Z 以及数字,把相似的数字和字母都剔除 无论大小写:将括号内的数字剔除(0,o,2,Z,1,i) $arr1 = range("a", ...