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. 有关javaEE及WebGIS的一些地址

    jdk:https://www.oracle.com/index.html eclipse:https://www.eclipse.org spring:http://repo.springsourc ...

  2. Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子

    在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...

  3. php常用的几个预定义变量

    __FILE__:返回所在路径文件名和文件名称 __DIR__:返回文件所在的完整目录 __LINE__:返回当前文件代码的行号 __CLASS__:返回当前类名 __FUNCTION__:返回当前方 ...

  4. katalon系列十四:执行Windows命令&获取项目路径

    Katalon Studio中也可以运行Windows命令执行一些系统操作. 根据官方文档,在test case中输入命令:cmd = 'del E:\\shot\\*.xlsx E:\\shot\\ ...

  5. java.lang.Boolean.valueOf(String s)

    简单说,就是s为true(这四个字母大小写任意)时,返回值为true,否则为false public class one { public static void main(String[] args ...

  6. 使用performance进行前端性能监控

    该文章仅作为自己的总结 1.performance.timing对象 navigationStart:当前浏览器窗口的前一个网页关闭,发生unload事件时的Unix毫秒时间戳.如果没有前一个网页,则 ...

  7. 《图解 HTTP 》阅读 —— 第二章

    第2章 简单的http协议 http 协议用于客户端和服务器端的通信. 请求访问文本或图像等资源的一端称为客户端,提供资源响应的一端称为服务器端. 请求报文: 响应报文: 为了能够处理大量的事务,ht ...

  8. 第四次ScrumMeeting博客

    第四次ScrumMeeting博客 本次会议于10月28日(六)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕. 1. 每个人的工作(有Issue的内容和 ...

  9. Serverless 架构的优点和缺点

    Serverless 的优势 在我使用 Serverless Framework 开发 AWS Serverless 应用的过程中,最方便的莫过于,第一次部署和第二次.第三次部署没有什么区别.只需要执 ...

  10. Python3【基础】-表达式与运算符

    一.什么是表达式? 1+2*3就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数.1+2*3计算的结果是7,计算结果可以存到一个变量中,即:res = 1 + 2 * 3. 所谓的表达式 ...