UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS)
题意分析
有2座塔,分别由不同长度的石块组成。现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少。
问题的实质可以转化为LCS(最长公共子序列)问题。
推荐一篇写的比较好的博文:
动态规划求解最长公共子序列(LCS)
核心的状态转移方程:
if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] +1;
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
代码总览
/*
Title:UVA.10066
Author:pengwill
Date:2017-2-16
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 105
using namespace std;
int a[nmax],b[nmax],dp[nmax][nmax];
int main()
{
//freopen("in.txt","r",stdin);
int n,m,cas = 0;
while(scanf("%d%d",&n,&m) && (n||m)){
printf("Twin Towers #%d\nNumber of Tiles : ",++cas);
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i = 1;i <=n; ++i) scanf("%d",&a[i]);
for(int i = 1;i <=m; ++i) scanf("%d",&b[i]);
for(int i = 1; i<=n; ++i)
for(int j = 1; j<=m;++j){
if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] +1;
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
printf("%d\n\n",dp[n][m]);
}
return 0;
}
UVA.10066 The Twin Towers (DP LCS)的更多相关文章
- UVA 10066 The Twin Towers(LCS)
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...
- uva 10066 The Twin Towers (最长公共子)
uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...
- UVA 10066 The Twin Towers
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- ZOJ 2059 The Twin Towers(双塔DP)
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
- The Twin Towers zoj2059 DP
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
随机推荐
- 多台服务器下同步文件夹数据(rsync+inotify)
网上有很多讲解rsync+inotify的教程,我就先贴出一个来大家去看吧,基本都是类似的. http://www.jb51.net/article/57011.htm 我就强调几点,按照上面的方法配 ...
- hdu2199Can you solve this equation?(解方程+二分)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Qt 5 最新信号和槽连接方式以及Lambda表达式
最近学习Qt,发现新大陆,这里做下记录. 主要内容就是原始Qt4的信号槽连接方式,以及Qt5新版的连接方式,还有件事简单演示一下lambda表达式的使用方式 代码如下 /* * 作者:张建伟 * 时间 ...
- 180605-Linux下Crontab实现定时任务
Linux下Crontab实现定时任务 基于Hexo搭建的个人博客,是一种静态博客页面,每次新增博文或者修改,都需要重新的编译并发布到Github,这样操作就有点蛋疼了,一个想法就自然而然的来了,能不 ...
- 第三篇 JavaScript基础
知识预览 BOM对象 DOM对象(DHTML) 实例练习 转:https://www.cnblogs.com/yuanchenqi/articles/5980312.html#_label2 一.Ja ...
- 第六阶段·数据库MySQL及NoSQL实践第1章·章节一MySQL数据库
01 课程介绍 02 数据库管理系统介绍 03 MySQL安装方式介绍及源码安装 04 MySQL安装后的基本配置 05 MySQL体系结构-服务器.客户端模型 06 MySQL体系结构-实例.连接层 ...
- 【cover-view、cover-image】 覆盖组件说明
cover-view.cover-image 这两类覆盖组件用于显示在一些特殊组件上方(map.video.canvas.camera.live-player.live-pusher). 这类组件一般 ...
- 142. O(1) Check Power of 2【LintCode by java】
Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return t ...
- 手动在Windows上创建kafka环境
一 主要内容请移步 参考文章 二 一点小问题 安装上面的参考文章完成配置,并且尝试传输消息后,我尝试去查看kafka的消息数据,目录是{logdir}/test-0/00000000000000.lo ...
- TensorFlow | ReluGrad input is not finite. Tensor had NaN values
问题的出现 Question 这个问题是我基于TensorFlow使用CNN训练MNIST数据集的时候遇到的.关键的相关代码是以下这部分: cross_entropy = -tf.reduce_sum ...