Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18765    Accepted Submission(s): 7946

Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. 
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. 
 
Sample Input
abcfbc abfcab
programming contest
abcd mnp
 
Sample Output
4
2
0
 
Source
 
Recommend
Ignatius   |   We have carefully selected several similar problems for you:  1087 1176 1058 1069 1421

 
之前做过好多次,一直不解其意,最近重温一遍。现在写下解题心得。
这道题的目的是求出a字符串和b字符串的最长公共子序列,用到动态规划。
动态规划的解法:
  先定义两个字符数组存储两个字符串
—— char a[1000]、b[1000];
  然后再定义一个二维数组,存储求解最终问题过程中产生的所有子问题的解
—— int dp[1001][1001];
最长公共子序列的状态转移方程为:
if(a[i]==b[j])  
    dp[i][j]=dp[i-1][j-1]+1;
else 
    dp[i][j]=dp[i-1][j]>dp[i][j-1]?dp[i-1][j]:dp[i][j-1];
根据以上写出程序即可。
另外摘取别人的一段对动态规划的解释:
【动态规划法】
  经常会遇到复杂的问题不能简单的分解成几个子问题,而会分解出一系列的子问题。简单的采用把大问题分解成子问题,并综合所有子问题的解求出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加。
  为了节约重复求相同子问题的时间,引入一个数组,不管他们是否对最终解有用,把所有子问题的解存于数组中,这就是动态规划法所采用的基本做法。
 
网易公开课的《算法导论》也有详细的讲解:
 
下面给出代码:
【C++】
 #include <iostream>

 using namespace std;
int dp[][];
int main()
{
//dp[i][j]代表着a取前i个字符和b取前j个字符时的最长公共子序列的大小
char a[],b[];
while(cin>>a>>b){
int i,j;
int al,bl;
for(i=;a[i]!='\0';i++); //计算a、b字符串长度
for(j=;b[j]!='\0';j++);
al=i;bl=j; for(i=;i<=al;i++) //dp[][]初始化
dp[i][]=;
for(i=;i<=bl;i++)
dp[][i]=; for(i=;i<=al;i++) //计算dp[][]
for(j=;j<=bl;j++){
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j] = dp[i-][j] > dp[i][j-] ? dp[i-][j] : dp[i][j-];
} cout<<dp[al][bl]<<endl;
}
return ;
}
【C】
 #include <stdio.h>
#include <stdlib.h>
int dp[][];
int main()
{
char a[],b[];
while(scanf("%s%s",a,b)!=EOF){
int i,j;
int al,bl;
for(i=;a[i]!='\0';i++);
for(j=;b[j]!='\0';j++);
al=i;bl=j;
for(i=;i<=al;i++)
dp[i][]=;
for(j=;j<=bl;j++)
dp[][j]=;
for(i=;i<=al;i++)
for(j=;j<=bl;j++){
if(a[i-]==b[j-])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = dp[i-][j] > dp[i][j-] ? dp[i-][j] : dp[i][j-];
}
printf("%d\n",dp[al][bl]);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1159:Common Subsequence(动态规划)的更多相关文章

  1. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  2. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  3. HDU 1159 Common Subsequence

    HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...

  4. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  5. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  6. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. HDU 1159 Common Subsequence(裸LCS)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. HDU 1159 Common Subsequence (动态规划、最长公共子序列)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

随机推荐

  1. stl-基本知识

    摘要:本文列出几个基本的STL map和STL set的问题,通过解答这些问题讲解了STL关联容器内部的数据结构,最后提出了关于UNIX/LINUX自带平衡二叉树库函数和map, set选择问题,并分 ...

  2. myBatis 实现用户表增删查改操作<方法2 加入接口>(最终版)

    这2种方法的区别:1.添加接口 2.运用接口来实现 其他的都一样 添加接口 //接口的名字和xml的名字一样,这样xml中的namespace就不用改 public interface UserMap ...

  3. &#x开头的是什么编码呢。浏览器可以解释它。如&#20013;&#22269;等同与中文"中国"?

    形如—— &#dddd; &#xhhhh; &#name; ——的一串字符是 HTML.XML 等 SGML 类语言的转义序列(escape sequence).它们不是「编码 ...

  4. 新浪微博客户端(3)-封装UIBarButtonItem

    单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装. UIBarButtonItem+Extensio ...

  5. 【转】 浅谈Radius协议

    浅谈Radius协议 2013-12-03 16:06 5791人阅读 评论(0) 收藏 举报  分类: Radius协议分析(6)  从事Radius协议开发有段时间了,小弟不怕才疏学浅,卖弄一下, ...

  6. 调用gluNurbsCurve绘制圆弧

    <OpenGL编程指南>第12章第3小结专门介绍调用GLU绘制NURBS曲线或曲面,很可惜的是并未给出绘制圆弧的例子.网上可以找到很多绘制整个园的例子,却没圆弧例子,自己瞎折腾了2个礼拜, ...

  7. HDOJ 1848 Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

  9. [Effective JavaScript 笔记]第18条:理解函数调用、方法调用及构造函数调用之间的不同

    面向对象编程中,函数.方法.类的构造函数是三种不同的概念. JS中,它们只是单个构造对象的三种不同的使用模式. 三种不同的使用模式 函数调用 function hello(username){ ret ...

  10. [Effective JavaScript 笔记]第48条:避免在枚举期间修改对象

    注册列表示例 一个社交网络有一组成员,每个成员有一个存储其朋友信息的注册列表. function Member(name){ this.name=name; this.friends=[]; } va ...