HDU1159-Common Subsequence
描述:
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的更多相关文章
- HDU-1159 Common Subsequence 最长上升子序列
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 解题报告 HDU1159 Common Subsequence
Common Subsequence Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU1159 && POJ1458:Common Subsequence(LCS)
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- 【水:最长公共子序列】【HDU1159】【Common Subsequence】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
随机推荐
- 【小白的java成长系列】——java ide 开发工具eclipse的操作
今天看了一下自己的博客文章,里面的内容还是比較杂的,有好多技术,有好多语言,都没有突出自己的强项,能够说,从博客里面,看不出我究竟是做哪块的..加上今天被授予了博客准专家勋章,自己想了一下,还是得梳理 ...
- 如何生成log新信息背景图片和在图片上添加水印
在图片上添加文字水印,其实就是要用到两个类, using System.Drawing; using System.Drawing.Drawing2D; 废话不多说了,直接上代 ...
- javascript 面向对象的继承的实现
JavaScript 中的面向对象的初步认识 上面这篇简单的记录了我对 JS面向对象实现的一点初步认识和了解,下面继续研究JS面向对象,实现继承和多态. 之前的学习我了解到了 :构造函数加属性,原型p ...
- 【转】 LESS CSS 框架简介
简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTML 一道,被广泛应用于万维网(World Wide Web)中.HTML 主要负责文档结构的定义,CSS 负责文档表现形式或样式的定义. ...
- oracle 如何重置用户密码
- SQL 语句中按照in语句原有的顺序进行排序
Access: ,,) order by instr(',1,5,3,',','&;id&;',') MSSQL: ,,) )))+',',',1,5,3,') MySQL: ,,) ...
- BZOJ 1087 互不侵犯king
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- [转载]给10万pv的WordPress选择最便宜高可用的硬件以及WordPress高并发支持
这些命令很长,但是希望可以让你很容易按照步骤创建一个全新的Linux服务器,使用Varnish, Nginx, W3 Total Cache, and WordPress来搭建一个WordPress博 ...
- Assets理解随笔
在PlayFramework中应用 在Play框架中提供的都是动态文件响应,前端工作内容大部分是静态文件.Assets大概起的就是这个作用. 默认路径看 conf/routes 里: # Map st ...
- 《离散数学之把妹要诀》的js实现
网上看到一篇有意思的文章<离散数学之把妹要诀> 就用JS写了上面所讲的配对方式: 首先设定变量 // 男生理想列表 var menPreference = { A: [1, 2, 3, 4 ...