Common Subsequence

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

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

题目链接: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+最长公共子序列】的更多相关文章

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

    题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...

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

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

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

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

  4. hdu 1159 Common Subsequence (最长公共子序列 +代码)

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

  5. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  6. HDU 1159 Common Subsequence 【最长公共子序列】模板题

    题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...

  7. HDU - 1159 Common Subsequence (最长公共子序列)

    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...

  8. 杭电1159 Common Subsequence【最长公共子序列】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 解题思路:任意先给出两个字符串 abcfbc abfcab,用dp[i][j]来记录当前最长的子 ...

  9. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  10. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

    题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Tot ...

随机推荐

  1. sourceTree git 忽略指定文件

    按照如下步骤执行(终端命令) 1. git status modified: LovegoMall.xcworkspace/xcuserdata/Tiny.xcuserdatad/xcdebugger ...

  2. Parallels Desktop 12 for Mac 破解版

    Parallels Desktop for Mac 是功能最强大灵活度最高的虚拟化方案,无需重启即可在同一台电脑上随时访问Windows和Mac两个系统上的众多应用程序.从仅限于PC的游戏到生产力软件 ...

  3. plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory

    装一台新服务器环境的时候,装uwsgi报错: plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or di ...

  4. IOS学习5——属性与成员变量

    [转]iOS中属性与成员变量的区别 ios中属性修饰符的作用 1. 属性用property声明 2. 简而言之,对于目前的ios开发,属性和成员变量的区别,完全可以不管. 3. 这个是历史原因造成的. ...

  5. iOS开发--SQLite重要框架FMDB的使用

    什么是FMDB: FMDB是一个和iOS的SQLite数据库操作相关的第三方框架.主要把C语言操作数据库的代码用OC进行了封装.使用者只需调用该框架的API就能用来创建并连接数据库,创建表,查询等. ...

  6. FastJson--阿里开源的速度最快的Json和对象转换工具

    示例 import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map ...

  7. CSS篇(上)

    紧接着HTML篇的CSS篇开啦,老铁们快来围观... 1.介绍一下标准的CSS盒子模型?低版本IE的盒子模型有什么不同? 1>有两种:IE盒子模型      W3C盒子模型 2>盒模型:内 ...

  8. bzoj 3669: [Noi2014] 魔法森林 LCT版

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

  9. PredictionIO+Universal Recommender快速开发部署推荐引擎的问题总结(1)

    1,PredictionIO如果用直接下载的0.11.0-incubating版本,存在一个HDFS配置相关的BUG 执行pio status命令时会发生如下的错误: -- ::, ERROR org ...

  10. find 命令详解

    find 作用:用于在文件树种查找文件,并作出相应的处理 (硬盘) 格式:find pathname -options [-print -exec -ok ...] 格式参数:pathname: fi ...