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 ...
随机推荐
- http性能测试点滴
WeTest 导读 在服务上线之前,性能测试必不可少.本文主要介绍性能测试的流程,需要关注的指标,性能测试工具apache bench的使用,以及常见的坑. 什么是性能测试 性能测试是通过自动化的测试 ...
- [JSON].result()
语法:[JSON].result() 返回:[True | False] 说明:用json字符串创建JSON实例时,如果该json字符串不是合法的json格式,会创建一个空的json实例.但是我们如果 ...
- post接口_form表单上传
上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中 LR中上传操作可以通过web_submit_data函数实现,支持录制要点:we ...
- [SHELL]输出目录下所有的可执行文件,批量创建用户
#!/bin/bash IFS=: for folder in $PATH #PATH变量分隔符为: do echo $folder echo ------------------ for file ...
- Dreamweaver CS5网页制作教程
说到Dreamweaver这个网页制作神器,不由得想起在学校里上的选修课,那是的我们只知道 table 布局,只知道构建网站最方便的是使用“所见即所得”编辑器.回忆一下,真的是很怀旧啊! 虽说咱现在大 ...
- appcrawler遍历工具常用方法
Usage: appcrawler [options] -a, --app <value> Android或者iOS的文件地址, 可以是网络地址, 赋值给appium的app选项 -c, ...
- 聊一聊session
最近从上家公司离职了,到了一家新公司,这几天一直在了解他们的项目,所以我自己的那个小项目也暂时搁浅了.. 今天差不多把他们的项目了解了,来院子写写我在这里边遇到的问题,影响最深刻的是seesion的. ...
- 【web前端开发】浏览器兼容性处理大全
1.居中问题 div里的内容,IE默认为居中,而FF默认为左对齐,可以尝试增加代码margin: 0 auto; 2.高度问题 两上下排列或嵌套的div,上面的div设置高度(height),如果di ...
- 手机站测试工具(node服务器)
最近在工作中遇到手机站测试的问题,于是就写了一个node服务外加一个第三方的转二维码功能,欢迎拍砖~ 项目地址:https://github.com/finderL/webserver
- Tic-Tac-Toe
Description Kim likes to play Tic-Tac-Toe. Given a current state, and now Kim is going to take his n ...