/*
*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的更多相关文章

  1. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  2. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. 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 ...

  5. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  6. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

  7. 最大公共字串LCS问题(阿里巴巴)

    给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...

  8. 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 ...

  9. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

随机推荐

  1. 爱挑剔的acm程序员 acmer

    2015-9-2 acmer敢于承认自己的错误,并积极改进.自信.有些酷爱运动,各个方面都很优秀:有些则认为出去“玩”就不是玩,有自己的小世界,玩电脑才是最爱. 2015-9-1 acmer都很聪明, ...

  2. 删除变长列字段后使用DBCC CLEANTABLE回收空间

    标签:SQL Server Reclaim space 收缩表 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lzf328.bl ...

  3. python之excel自动报表

    一.自动报表前期工作: 需要安装XlsxWriter模块,可以从github上下载以后解压.setup.py install. 二.程序: #!/usr/bin/env python # -*- co ...

  4. 零配置简单搭建SpringMVC 项目

    SpringMVC是比较常用的JavaWeb框架,非常轻便强悍,能简化Web开发,大大提高开发效率,在各种Web程序中广泛应用.本文采用Java Config的方式搭建SpringMVC项目,并对Sp ...

  5. 初识CEF

    介绍 CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chromium项目主要是为Google Chrome应用开 ...

  6. hive1.2.1实战操作电影大数据!

    我采用的是网上的电影大数据,共有3个文件,movies.dat.user.dat.ratings.dat.分别有3000/6000和1百万数据,正好做实验. 下面先介绍数据结构: RATINGS FI ...

  7. python之路3:

    class set(object): """ set() -> new empty set object set(iterable) -> new set o ...

  8. 安装软件时出现error 1337 【添加权限】

    Error 1317 An error occurred while attempting to create the directory Drive Name:\Folder Name https: ...

  9. Ubuntu中配置Thunderbird登录qq邮箱

    1.打开thunderbird 2.开启QQ邮箱pop功能 登录网页版QQ邮箱(email.qq.com) 设置---->>账户---->> 3.配置thunderbird 注 ...

  10. Linux json解析jq

    jq是一个linux下很方便的json解析器,平时看json文件都是一行混在一起的,非常不适合阅读. https://stedolan.github.io/jq/download/ 直接到官网上下载, ...