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; ( ...
随机推荐
- 产品专家Marty Cagan:不做仅仅会编码的人
Marty Cagan是享有世界声誉的产品管理专家,曾担任Netscape副总裁.eBay产品管理及设计高级副总裁. 近日,记者在"PM-China首届产品经理高峰论坛"上对他做了 ...
- C#.NET学习笔记7--11---算术运算符,变量赋值,变量的交换,布尔表达式1,布尔表达式2
C#.NET学习笔记7---算术运算符 2013/9/6 技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com 1.Consol ...
- DevExpress.XtraCharts.chartControl
private Dictionary<string, double> chartPieDataDic = new Dictionary<string, double>(); p ...
- 我的小前端 (1)—— 安卓机和ios机的区别
没有什么特别新技术,就是记录我做移动端遇到的问题 2016-02-16 微信,支付宝和APP都会遇到这些问题 一.安卓机和ios机的区别 1.常用 <head> <me ...
- Excel中公式的绝对引用和相对引用单元格
在Excel的表格中,非常常用的就是公式里的绝对引用和相对引用了,具体情况请看下列表格吧. 步骤1 打开做好的excel表格.公式中的相对单元格引用是基于包含公式和单元格引用的单元格的相对位置,若公式 ...
- WinForm DataGridView看似刷新的问题
昨天同事winform遇到一个问题, 窗体上有一个时间控件,和一堆文本,下拉控件,时间控件是每秒都在动态走的 窗体下发一个DataGridView 控件显示保存后的数据 保存的数据库是在另一台机器B上 ...
- [转]取代cookie的网站追踪技术:”帆布指纹识别”初探
[前言] 一般情况下,网站或者广告联盟都会非常想要一种技术方式可以在网络上精确定位到每一个个体,这样可以通过收集这些个体的数据,通过分析后更加精准的去推送广告(精准化营销)或其他有针对性的一些活动.C ...
- ES6笔记③
1.查找关键字 includes(); 返回布尔值 //①:includes -->代替-->indexof-->返回布尔值 var str = "769909303&q ...
- Java核心技术卷1Chapter7笔记 图形程序设计
Swing是指被绘制的用户界面类,AWT是指像事件处理这样的窗口工具箱的底层机制. SWT,JavaFX是可能的代替技术. 创建框架 在Java中,顶层窗口(就是没有包含在其他窗口中的窗口)被称为框架 ...
- Java语言导学笔记 Chapter 9 IO
java.io 9.1.1 字符流 Reader为读取器(reader)提供API和部分实现,读取器是读取16位字符的流: Writer为写出器(writer)提供API和部分实现,写出器是写16位字 ...