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)的更多相关文章

  1. UVA 10066 The Twin Towers(LCS)

    Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...

  2. uva 10066 The Twin Towers (最长公共子)

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  3. UVa 10192 - Vacation &amp; UVa 10066 The Twin Towers ( LCS 最长公共子串)

    链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...

  4. UVA 10066 The Twin Towers

    裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...

  5. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  6. LightOJ1126 Building Twin Towers(DP)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...

  7. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  8. ZOJ 2059 The Twin Towers(双塔DP)

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  9. The Twin Towers zoj2059 DP

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

随机推荐

  1. PLSQL-包函数存储过程

    包: 包是PLSQL中多个单元的逻辑组合,他将过程组合在一个包内容,以供用户调用,使用后,不需要程序员频繁的修改程序,可以保持程序的逻辑完整性,对包中的过程重新定义或者编译,以便修改部分功能,从而更好 ...

  2. hdu1069Monkey and Banana(动态规划)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. adb获取设备的序列号

    用数据线连接手机, 打开开发者模式, 并赋予相关权限, 在CMD命令行输入: adb devices 第一个参数即为设备的序列号, 第二个参数device表示设备的状态是在线.

  4. JMeter自学笔记2-图形界面介绍

    一.写在前面的话: 上篇我们已经学会了如何安装JMeter和打开JMeter,那么这篇我们将对JMeter的图形界面做一个简单的介绍.大家只要简单的了解即可,无需死记硬背,在今后的学习和使用中慢慢熟悉 ...

  5. Python简要标准库(2)

    集合 堆 和 双端队列 1.集合 创建集合 s = set(range(10)) 和字典一样,集合元素的顺序是随意的,因此不能以元素的顺序作为依据编程 集合支持的运算 a = set([1,2,3]) ...

  6. (C#)设计模式之状态模式

    1.状态模式 当一个对象的内在状态改变时允许改变其行为,这个对象看起像是改变了其类. *状态模式主要解决的是当控制一个对象的状态转换的条件表达式过于复杂时,可以将状态的判断逻辑转移到表示不同状态的一系 ...

  7. org.apache.spark.launcher.Main源码分析

    public static void main(String[] argsArray) throws Exception { //org.apache.spark.launcher.Main chec ...

  8. ZOJ 3686 A Simple Tree Problem(线段树)

    Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...

  9. 2017软工第二次作业 - 本周PSP(补交)

    每周例行报告 1.本周PSP 2. 本周进度条 3.累计进度图 4. 本周PSP饼状图

  10. 解决android invalid symbol: 'switch'

    http://stackoverflow.com/questions/16728178/unable-to-compile-project-in-android-studio-gradle-inval ...