/*
*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. 查找树ADT

    通过二叉查找树实现排序的例程 /** * 无论排序的对象是什么,都要实现Comparable接口 * * @param <T> */ public class BinaryNode< ...

  2. 【iOS开发】在 App 中加入 AdMob 广告 - 入门介绍与编程技巧

    前言 虽然在App中加入广告来盈利是比较低级的商业化方式,但对于个人开发者或者小团队开发者来说,做出一个简单易用的免费小工具(举个栗子),在里面加入一些广告,如果用的人多,也是可以带来一些可观的收入的 ...

  3. android使用默认程序进行图片拍照已经裁剪,以及设备读取

    //代码如下: package com.cbsw.yulechangsuo.activity; import java.io.File;import java.io.FileInputStream;i ...

  4. Thinkphp框架回顾(三)之怎么实现平常的sql操作数据库

    1.首先简单介绍一下我们的数据库,thinkphp数据库下有一个tp_user表,然后有四个字段....id,username,password,sex 我们今天的任务就是在Thinkphp下将数据调 ...

  5. mybatis使用generator生成对应的model、mapping配置文件、dao

    http://blog.csdn.net/fengshizty/article/details/43086833 使用时 补充 输入命令:cd d:\generator  敲回车 再输入命令:d: 敲 ...

  6. easyui validatebox 验证类型

    required: "必选字段",        remote: "请修正该字段",        email: "请输入正确格式的电子邮件" ...

  7. HTML5 canvas处理图片的各种效果,包括放大缩小涂鸦等

    http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201502151385.html jQuery 缩放 旋转 裁剪图片 Image Cropper ...

  8. 20161127-adt bundle

    1.adt.exe 路径:E:\software\adt-bundle-windows-x86-20131030\sdk\platform-tools\adt.exe 配置环境变量 命令: adb d ...

  9. 使用注解方式生成Hibernate映射文件

    @Entity:表示是一个hibernate的实体类 @Table:表示实体类和表的对应关系 @Id:表示是数据库中的主键 @Column:在数据表中描述的对应的列的信息 属性名是根据get方法,数据 ...

  10. BW知识问答锦集2

    PM面试分为BW.BO两部分,根据顾问的简历和应聘的岗位所侧重的问题不同. BW包括基础知识.增量.增强.LO抽取.数据源. BO包括 CR.CR.WEBI.UNI. 一. 基础知识 技术面试 1. ...