描述:

  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 anynumber 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.

代码:

  设序列X={x1,x2,…,xm}和Y={y1,y2,…,yn}的最长公共子序列为Z={z1,z2,…,zk} ,则

    (1)若xm=yn,则zk=xm=yn,且zk-1是xm-1和yn-1的最长公共子序列。

    (2)若xm≠yn且zk≠xm,则Z是xm-1和Y的最长公共子序列。

    (3)若xm≠yn且zk≠yn,则Z是X和yn-1的最长公共子序列。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
#define N 1000 int main(){
char x[N],y[N];
int dp[N][N];
while ( scanf("%s%s",&x,&y)!=EOF ){
//TLE:memset(dp,0,sizeof(dp));
for( int i=;i<N;i++ ){
dp[][i]=;dp[i][]=;
} for( int i=;i<=strlen(x);i++ ){
for( int j=;j<=strlen(y);j++ ){
if( x[i-]==y[j-] )
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",dp[strlen(x)][strlen(y)]);
}
system("pause");
return ;
}

HDU1159-Common Subsequence的更多相关文章

  1. HDU-1159 Common Subsequence 最长上升子序列

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

  2. 解题报告 HDU1159 Common Subsequence

    Common Subsequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. HDU1159 && POJ1458:Common Subsequence(LCS)

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

  4. 【水:最长公共子序列】【HDU1159】【Common Subsequence】

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

  5. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  6. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

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

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

  9. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  10. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

随机推荐

  1. Hadoop学习之编译eclipse插件

    近期准备開始学习Hadoop1.2.1的源码,感觉最好的方法还是能够在运行Hadoop及hadoop作业时跟踪调试代码的实际运行情况.因为选择的IDE为eclipse,所以准备编译一下hadoop的e ...

  2. oauth2认证

    using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Con ...

  3. 树形dp-hdu-4714-Tree2cycle

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4714 题目意思: 给一棵树,去掉一条边和增加一条边的花费都为1,求最小的花费,使该树变成一个环. 解 ...

  4. 做ie8css样式时浏览器默认杂项模式遇到的一个小坑

    1 进行ie浏览器的样式兼容的时候,首先要确保打开浏览器浏览网页的时候的文本模式要为当前浏览器的"标准模式",注意<!DOCTYPE html>不缺失不错误,以免浏览器 ...

  5. globalCompositeOperation 学习

    globalCompositeOperation globalCompositeOperation即Canvas中的合成操作. 1.source-over 这是默认值,他表示绘制的图形将画在现有画布之 ...

  6. Android 支付宝接入时常见的问题

    1.概述 首先说明下,Android支付宝接入用的是快捷支付,下载地址是https://b.alipay.com/order/techService.htm    支付宝移动接入地址https://b ...

  7. HDU 2685 I won't tell you this is about number theory

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685 题意:求gcd(a^m - 1, a^n - 1) mod k 思路:gcd(a^m - 1, ...

  8. Linux命令学习之shift命令

          位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift ...

  9. FAQ:Python环境变量配置

    Python安装安装成,需要配置环境变量: 默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1. 首先需要在系统中注册 ...

  10. J2SE知识点摘记(十三)

    1.        字节流 InputStream(输入字节流)是一个定义了java流式字节流输入模式的抽象类.该类的所有方法在出错时都会引发一个IOExcepiton异常. Void close() ...