http://acm.hdu.edu.cn/showproblem.php?pid=1159

Common Subsequence

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

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
 
动态规划中的最长公共子序列。用dp[i][j]保存第一个字符串前i个字符和第二个字符串前j个字符的公共子序列长度。
如果str1[i]==str2[j],dp[i][j]=dp[i-1][j-1]。
如果str1[i]!=str2[j],dp[i][j]=max(dp[i-1][j],dp[i][j-1])
上述即是动态规划的状态转移公式。
 
 #include <stdio.h>
#include <string.h> using namespace std; int dp[][]; char str1[], str2[]; int max(int a, int b)
{
if(a>b)
return a;
else
return b;
} int main()
{
while(scanf("%s %s", str1, str2)!=EOF)
{ int len1=strlen(str1);
int len2=strlen(str2); for(int i=; i<=len1; i++) dp[i][]=;
for(int j=; j<=len2; j++) dp[][j]=; for(int i=; i<=len1; i++)
for(int j=; j<=len2; j++)
{
if(str1[i-]!=str2[j-])
{
dp[i][j]=max(dp[i-][j], dp[i][j-]); } else
{
dp[i][j]=dp[i-][j-]+;
}
} printf("%d\n",dp[len1][len2]);
} return ;
}

hdu-题目1159:Common Subsequence的更多相关文章

  1. HDU 1159 Common Subsequence

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

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

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

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

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

  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(裸LCS)

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

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

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

  7. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  8. HDU 1159 Common Subsequence【dp+最长公共子序列】

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

  9. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. hdu 1159 Common Subsequence(LCS)

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

随机推荐

  1. svg vs canvas

    http://fabricjs.com/ https://github.com/ecomfe/zrender http://raphaeljs.com/

  2. 20155207王雪纯 2006-2007-2 《Java程序设计》第二周学习总结

    20155207王雪纯 2006-2007-2 <Java程序设计>第二周学习总结 教材学习内容总结 整数类型:short(占2字节).int()占4字节.long(占8字节) " ...

  3. 第三次预作业20155231邵煜楠:虚拟机上的Linux学习

    java第三次预作业--虚拟机初体验(学习记录) 学习在笔记本上安装Linux操作系统 通过老师给予的官网链接,下载了VirtualBox-5.1.14-112924-win和Ubuntu-16.04 ...

  4. 20155313 2016-2017-2 《Java程序设计》第十周学习总结

    20155313 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就 ...

  5. Win SERVER 2008 许可证激活失败,系统重启问题

    服务器系统win server2008 R2 SP1,频繁重启,查看日志 有显示 许可证激活(slui.exe)失败,错误代码如下:0x800401F9 和 Windows 许可证激活失败.错误 0x ...

  6. mac php版本切换

    mac os 中自带php版本,但是很多扩展是不带的. 这个网站: http://php-osx.liip.ch/提供了几乎所有的php版本 通过输入 curl -s http://php-osx.l ...

  7. autoreleasepool 自动释放池的理解

    常见的面试题:以下代码存在什么样的问题?应该如何改进? for (int i = 0; i < 100000; i++) { NSString *str = @"abc"; ...

  8. Wince 中访问WCF服务

    由于本文并非WinCE开发普及篇,所以一些WinCE开发和WCF开发的基础还请移步百度和谷歌寻找答案,然后结合本文开发出WinCE中如何访问WCF,谢谢. 开发环境 IDE:Visual Studio ...

  9. 【转】RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第二篇【原理】

    接着上一篇,我们开始聊聊APPIUM的框架和运行模式.废话不多说直接上图. 1.首先自动化脚本通过RobotFrameWork将命令传递给Appium的客户端: 2.然后[Appium的客户端]将接受 ...

  10. Python基本编程题

    问题1:仅使用 Python 基本语法,即不使用任何模块,编写 Python 程序计算下列数学表达式的结果并输出,小数点后保留3位.‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬ ...