HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 39559 Accepted Submission(s): 18178
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.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159
题目大意:给出两个字符串,求两个字符串的最长公共字串。
思路:这题是简单的动态规划题。以题目的第一组测试数据为例。(参考A_Eagle的题解)
abcfbc abfcab。

可以看出:
dp[i][j]=dp[i-1][j-1]+1;(a[i]==b[j])
dp[i][j]=max(dp[i-1][j],dp[i][j-1])(a[i]!=b[j]);
n由于F(i,j)只和dp(i-1,j-1), dp(i-1,j)和dp(i,j-1)有关, 而在计算dp(i,j)时, 只要选择一个合适的顺序, 就可以保证这三项都已经计算出来了,
这样就可以计算出dp(i,j). 这样一直推到dp(len(a),len(b))就得到所要求的解了.
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
char s1[],s2[];
int dp[][];
int main()
{
int len1,len2;
while(scanf("%s%s",s1+,s2+)!=EOF)
{
memset(dp,,sizeof(dp));
len1=strlen(s1+),len2=strlen(s2+);
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-]);
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}
HDU 1159 Common Subsequence【dp+最长公共子序列】的更多相关文章
- hdu 1159 Common Subsequence(最长公共子序列,DP)
题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...
- hdu 1159 Common Subsequence(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1159 Common Subsequence (最长公共子序列 +代码)
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
- HDU 1159 Common Subsequence 【最长公共子序列】模板题
题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...
- HDU - 1159 Common Subsequence (最长公共子序列)
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...
- 杭电1159 Common Subsequence【最长公共子序列】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 解题思路:任意先给出两个字符串 abcfbc abfcab,用dp[i][j]来记录当前最长的子 ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)
题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Tot ...
随机推荐
- Google Chrome谷歌/火狐/Safari浏览器开发者工具基本使用教程
前言 在阅读下面内容之前,那么些简单的了解浏览器开发者工具到底是什么东西,到底有什么用途. 浏览器开发者工具到底是什么? 其实简单的说,浏览器开发者工具就是给专业的web应用和网站开发人员使用的工具, ...
- C#程序打包安装部署
今天为大家整理了一些怎样去做程序安装包的具体文档,这些文档并不能确保每个人在做安装包的时候都能正确去生成和运行,但是这些文档的指导作用对于需要的朋友来说还是很有必要的,在实际产品的安装部署过程中可能有 ...
- ArcGIS 网络分析[1.4] 制作点线要素时需要注意的地方
有很多同学虽然成功做好了网络数据集,但是分析时会出现这样的问题: 这是为什么呢? 这有三个可能的原因: 1. 两个点之间所有的道路没有连通(问题出在点.线数据上). 2. 网络数据集出现了孤立的点位置 ...
- ECMAScript中的两种属性
数据属性 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性一般用于存储数据数值. 数据属性有4个描述其行为的特征. configurable:true/false,是否可以通过del ...
- gitlab 远程 定时备份
=============================================== 2017/11/1_第2次修改 ccb_warlock 更新 ...
- 浅谈JavaScript的面向对象程序设计(一)
面向对象的语言有一个标志,他们都有类的概念,通过类可以创建多个具有相同属性和方法的对象.但是JavaScript中没有类的概念,因此JavaScript与其他的面向对象语言还是有一定区别的.JavaS ...
- SpringBoot入坑-项目搭建
对于学过三大框架的小童鞋,从今天开始给大家带来一套新的框架学习,相信对于做程序的小童鞋一定有所耳闻,作为下一代java开发框架springboot,减去了繁琐的xml配置,相信用过spring.sta ...
- 线程安全Dictionary
public abstract class ReadFreeCache<TKey, TValue> { protected ReadFreeCache() : this(null) { } ...
- 转:java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
以下是从网上找到的关于堆空间溢出的错误解决办法: Java.lang.OutOfMemoryError: Java heap space =============================== ...
- amaze UI 笔记 - JS
导航添加依据 http://amazeui.org/javascript 下面内容属学习笔记,如有理解偏差和错误请留言相告,感谢!* =(官网这块写的很详细) 一 .UI增强 1.警告框 显示可关闭的 ...