描述:

  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. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

  2. C++Primer 中间Sales_items.h头文件

    #ifndef SALESITEM_H #define SALESITEM_H #include <iostream> #include <string> class Sale ...

  3. node.js(七) 子进程 child_process模块

    众所周知node.js是基于单线程模型架构,这样的设计可以带来高效的CPU利用率,但是无法却利用多个核心的CPU,为了解决这个问题,node.js提供了child_process模块,通过多进程来实现 ...

  4. ORA-01950: 对表空间 'NAMETABLESPACE' 无权限

    只要将Role下的Resource权限赋予给当前用户即可解决上述问题.

  5. UTL_FILE 的用法

    UTL_FILE 的用法   UTL_FILE 是用来进行文件IO处理的专用包,使用这外包的注意事项如下: 1. 生成的文件好象只能放置在DATABASE所在的服务器路径中. 2. 生成的文件如何DO ...

  6. JQuery表格展开与内容筛选

    单击分类的时候,可以关闭打开相对应的内容.例如点击前台设计组,则只显示前台设计组的内容.再次点击则收缩. 筛选的话就是匹配输入框的内容,如果某行数据存在,则显示出来. <html> < ...

  7. 动态include与静态include的区别

    搬一下以前写的 个人总结: 动态INCLUDE 用jsp:include动作实现 <jsp:include page="included.jsp" flush="t ...

  8. Generative Learning algorithms

    "generative algorithm models how the data was generated in order to categorize a signal. It ask ...

  9. [原创]obj-c编程16:键值编码(KVC)

    原文链接:obj-c编程16:键值编码(KVC) 我们可以借助obj-c中的键值编码(以后简称KVC,Key-Value Coding)来存取类的属性,通过指定所要访问的属性名字符串标示符,可以使用存 ...

  10. OSG坐标系统

    1.世界坐标         世界坐标系描述的是整个场景中所有的对象,可以理解为绝对坐标系,所有对象的位置都是绝对坐标.从整体上考虑,它为所有对象的位置提供一个绝对的参考标准,从而避免了物体之间由于独 ...