Common Subsequence

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

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
 

题意就是求两个字符串的最长公共子序列的长度。

LCS入门,传送门1:http://blog.csdn.net/liufeng_king/article/details/8500084

传送门2:http://blog.csdn.net/orbit/article/details/6717125

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+;
char s1[N],s2[N];
int dp[N][N];
int len1,len2;
void fun(){
memset(dp,,sizeof(dp));
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
if(s1[i-]==s2[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
}
int main(){
while(~scanf("%s%s",&s1,&s2)){
len1=strlen(s1);
len2=strlen(s2);
fun();
printf("%d\n",dp[len1][len2]);
}
return ;
}

HDU1159-Common Subsequence-LCS的更多相关文章

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

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

  2. HDU1159 && POJ1458:Common Subsequence(LCS)

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

  3. Common Subsequence LCS

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...

  4. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...

  5. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  6. HDU-1159 Common Subsequence 最长上升子序列

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

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

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

  8. 解题报告 HDU1159 Common Subsequence

    Common Subsequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. 解题报告:hdu1159 common consequence LCS裸题

    2017-09-02 17:07:42 writer:pprp 通过这个题温习了一下刚学的LCS 代码如下: /* @theme:hdu1159 @writer:pprp @begin:17:01 @ ...

  10. Longest Common Subsequence (LCS)

    最长公共子序列(LCS)是经典的DP问题,求序列a[1...n], b[1..m]的LCS. 状态是DP[i][j],表示a[1..i],b[1..j]的LCS. DP转移方程是 DP[i][j]= ...

随机推荐

  1. javascript + sql编写SQL客户端工具tabris

    祝大家2018新年快乐, 前不久发现了一个创意的脚本JtSQL(java编写) 开源地址为:https://github.com/noear/JtSQL JtSQL 特点:*.结合了JS.SQL.模板 ...

  2. Docker(九):Docker容器卷插件

    1.Convoy 1.1 安装 [root@MediaServer tmp]# tar xvf convoy.tar.gz convoy/ convoy/convoy-pdata_tools conv ...

  3. css3整理-方便查询使用

    最近详细地研究了CSS3的相关内容,并整理了这个文档,方便以后查询使用,分享给大家. 案例代码大家可以下载参考下:https://gitee.com/LIULIULIU8/CSS3 1.边框属性bor ...

  4. Jasperreports以及iReport4.5报表PDF导出字体完美解决方案

    在使用Jasperreports以及iReport设计报表时,导出PDF是一个常见的需求.网上解决PDF导出中文显示问题相关的文章很多,无非就是设置控件的pdf font name和pdf encod ...

  5. Q:java中serialVersionUID的作用

    @转载自:http://www.cnblogs.com/guanghuiqq/archive/2012/07/18/2597036.html   简单来说,Java的序列化机制是通过在运行时判断类的s ...

  6. 解决CUICatalog: Invalid asset name supplied问题

    这个问题其实是老问题,产生原因就是因为在使用的时候 [UIImage imageNamed:]时,图片不存在或者传入的图片名为nil.

  7. TurnipBit开发板“趣味赛”:平衡力大比拼

    让孩子在快乐自由的游戏中培养编程思维 平衡力大挑战是我们经常经常玩的的一个小游戏,脑补画面的话比较常见的是单腿平衡力大比拼,摇晃幅度小者胜利.游戏好玩归好玩,但是想要公平判断胜负却不容易.下面就教大家 ...

  8. 一个简单JDK动态代理的实例

    动态代理的步骤: 创建一个实现了InvocationHandler接口的类,必须重写接口里的invoke()方法. 创建被代理的类和接口 通过Proxy的静态方法 newProxyInsatance( ...

  9. 【读书笔记】【深入理解ES6】#7-Set集合和Map集合

    ES6新标准中将Set集合和Map集合添加到JS中. ES5中Set集合和Map集合 在ES5中,开发者们用对象属性来模拟这两种集合. var set = Object.create(null); s ...

  10. 关于java中,json字符串转集合和对象,或者集合转json字符串的解决方法

    1 List集合转成json字符串 //Java集合 List<String> list = new ArrayList<String>(); list.add("a ...