Common Subsequence

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

Total Submission(s): 28494    Accepted Submission(s): 12735

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。以下给出第一种情况的DP路线,例如以下图。

希望能够帮助大家。


AC代码:

#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b? a:b)
char a[1000],s[1000];
int dp[1000][1000];
int main()
{
int i,j,k;
while(scanf("%s%s",a,s)!=EOF)
{
memset(dp,0,sizeof(dp));
int l=strlen(a);
int le=strlen(s);
for(i=1;i<=l;i++)
{
for(j=1;j<=le;j++)
if(a[i-1]==s[j-1])//推断左側和上側字符是否相等
dp[i][j]=dp[i-1][j-1]+1;//把左上側的dp值+1
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//取左側或上側的最大dp值
}
printf("%d\n",dp[l][le]);
}
return 0;
}

     

hdoj 1159 Common Subsequence【LCS】【DP】的更多相关文章

  1. HDOJ 1159 Common Subsequence【DP】

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

  2. HDU 1159 Common Subsequence(裸LCS)

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

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

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

  4. HDOJ --- 1159 Common Subsequence

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

  5. Hdoj 1159.Common Subsequence 题解

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

  6. hdu 1159 Common Subsequence(LCS)

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

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

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

  9. HDU 1159 Common Subsequence

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

随机推荐

  1. 支持并发的httpclient(基于tcp连接池以及netty)

    闲来无事,将曾经自己写的一个库放出来吧. . 有的时候会有这样子的需求: (1)serverA通过HTTP协议来訪问serverB (2)serverA可能会并发的像B发送非常多HTTP请求 类似于上 ...

  2. hdu 5318 The Goddess Of The Moon 矩阵高速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...

  3. [Android] 使用Matrix矩阵类对图像进行缩放、旋转、对照度、亮度处理

        前一篇文章讲述了Android拍照.截图.保存并显示在ImageView控件中,该篇文章继续讲述Android图像处理技术,主要操作包含:通过打开相冊里的图片,使用Matrix对图像进行缩放. ...

  4. Liunx-php7安装swoole扩展

    Liunx-php7安装swoole扩展 标签(空格分隔): php 下载包 https://github.com/swoole/swoole-src/releases 安装过程 直接wget也行直接 ...

  5. 5.不用拷贝的对象可以用ref

    #include <iostream> #include <string> #include <boost/bind.hpp> #include <boost ...

  6. 利用中间件 mysql_proxy 完成 mysql 的负载均衡和读写分离

      安装 mysql_proxy       cd /usr/local/src       wget http://mysql.cdpa.nsysu.edu.tw.Downloads/MySQL - ...

  7. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

  8. Linux 部署项目经验总结

    [通用命令]  1.创建文件夹 mkdir -p xxx 2.解压包 tar -zxvf xxxx.tar.gz  3.缩文件 tar zcvf 压缩包名称.tar.gz 要压缩的文件  4.动命令  ...

  9. unity 5.6.1 Oculus手柄输入问题

    unity文档中提到 轴的 ID 是5和6,但是测试后发现,ID是6和7,很坑 void Update () { if (Input.GetKeyDown(KeyCode.JoystickButton ...

  10. ECNUOJ 2143 端午节快乐

    端午节快乐 Time Limit:1000MS Memory Limit:65536KBTotal Submit:1720 Accepted:868 Description  有一段有趣的传说.公元前 ...