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. c++命名规范与代码风格

    http://blog.sina.com.cn/s/blog_a3a8d0b1010100uw.html http://www.cnblogs.com/len3d/archive/2008/02/01 ...

  2. OneNote

    OneNote导致生成OneNote Table Of Contents.onetoc2 https://answers.microsoft.com/en-us/msoffice/forum/all/ ...

  3. 英语音乐---二、Burning

    英语音乐---二.Burning 一.总结 一句话总结:Burning - Maria Arredondo 玛丽亚·亚瑞唐多(Maria Arredondo),1985年7月6日出生于文内斯拉小镇,挪 ...

  4. zookeeper的理解与概述

    文章转自https://www.cnblogs.com/likehua/p/3999600.html  感谢博主 文章转自:http://www.aboutyun.com/thread-9266-1- ...

  5. [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)

    题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...

  6. BZOJ 球形空间产生器 解题报告(高斯消元)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1013 1013: [JSOI2008]球形空间产生器sphere 有一个球形空间产生器能 ...

  7. Kali linux 2016.2(Rolling)里Metasploit的OpenVAS

    不多说,直接上干货! 关于OpenAVS的概念,我这里不多赘述. 前提得,大家要先安装好OpenVAS!!! 我们都知道,BT5中已经预先安装好了OpenVAS网络漏洞扫描工具,我们只需进行一些配置即 ...

  8. myisam和innodb的qubie

    1.Myisam 支持锁表,innoDB 支持行锁. 2.innoDB 和 BDB 支持事务. 3.Myisam 与 innoDB 索引的区别:      Myisam 无论是主键索引还是其他索引,索 ...

  9. 洛谷P1164 小A点菜 && caioj 1410 动态规划1:点菜(背包方案问题)

    方程很简单 f[0] = 1 f[j] += f[j-w[i]] #include<cstdio> #define REP(i, a, b) for(int i = (a); i < ...

  10. Win10平台下通过VMware虚拟机安装Win7、Ubuntu、Mac

    1.安装VMware14.1.1 下载地址:https://download.csdn.net/download/jasonczy/10611423 产品秘钥: CG54H-D8D0H-H8DHY-C ...