题目链接: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): 25416    Accepted Submission(s):
11276

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
 
题目大意:找到最长公共子序列,如:abcfbc abfcab 这个的最长公共子序列为abfc或abcb 所以输出4!
 
题目思路:定义一个dp[i][j]的二维数组。用来表示最长的公共子序列数。i表示第一个字符串的开始位置,j表示第二个字符串的开始位置。也就是从后向前推。dp[0][0]表示从第0个到第n个的最长公共子序列数,因为结尾就是n。dp[n][n]表示的是第n个到第n个的最长公共子序列。
 
详见代码。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int dp[][]; int main()
{
char a[],b[];
while (~scanf("%s%s",a,b))
{
int len1=strlen(a);
int len2=strlen(b);
memset(dp,,sizeof(dp));
for (int i=len1-;i>=;i--)
{
for (int j=len2-;j>=;j--)
{
if (a[i]==b[j])
dp[i][j]=dp[i+][j+]+;
else
dp[i][j]=max(dp[i+][j],dp[i][j+]);
}
}
printf ("%d\n",dp[][]);
}
return ;
}
 

hdu 1159 Common Subsequence(最长公共子序列 DP)的更多相关文章

  1. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  2. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  3. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  4. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  5. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  6. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

  7. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  8. LCS(Longest Common Subsequence)最长公共子序列

    最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...

  9. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

随机推荐

  1. nginx 设置默认虚拟 host

    nginx 设置默认虚拟 host listren 80 default_server

  2. volatile并不能保证数据同步、只能保证读取到最新主内存数据

    在 java 垃圾回收整理一文中,描述了jvm运行时刻内存的分配.其中有一个内存区域是jvm虚拟机栈,每一个线程运行时都有一个线程栈, 线程栈保存了线程运行时候变量值信息.当线程访问某一个对象时候值的 ...

  3. NAT穿透(UDP打洞)

    1.NAT(Network Address Translator)介绍 NAT有两大类,基本NAT和NAPT. 1.1.基本NAT 静态NAT:一个公网IP对应一个内部IP,一对一转换 动态NAT:N ...

  4. 关于"作数类型冲突: nvarchar 与 image 不兼容"的问题

    数据库如果是Image类型,当执行插入语句时,如果插入的值是DBNull.Value时提示:操作数类型冲突: nvarchar 与 image 不兼容; 出现这个问题的原因是没有指定DbType的原因 ...

  5. hdu5696区间的价值 -- 2016"百度之星" - 初赛(Astar Round2B)

    Problem Description 我们定义“区间的价值”为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1). 现在聪明的杰西想要知道,对于长度为k的区 ...

  6. 算法语言Scheme修订6报告 R6RS简体中文翻译

    算法语言Scheme修订6报告 R6RS简体中文翻译 来源 https://r6rs.mrliu.org/   MICHAEL SPERBERR. KENT DYBVIG, MATTHEW FLATT ...

  7. 【刷题】BZOJ 2179 FFT快速傅立叶

    Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...

  8. [2018国家集训队][UOJ449] 喂鸽子 [dp+组合数学]

    题面 传送门 思路 首先,这道题是可以暴力min-max反演+NTT做出来的......但是这个不美观,我来讲一个做起来舒服一点的做法 一个非常basic的idea:我们发现在一只鸽子吃饱以后再喂给它 ...

  9. 【转】TCP拥塞控制,慢启动、拥塞避免、快重传以及快恢复

    转自:http://blog.csdn.net/yusiguyuan/article/details/22847787 注:本文绝大部分是来自转载的博客,还补充了少量内容. 一.TCP的拥塞控制 拥塞 ...

  10. AOJ. 数组训练.2016-11-17

    A题 #include <stdio.h> #include <stdlib.h> #define max 1000 __int64 a[max] = {0,1,1}; int ...