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. java后台接受web前台传递的数组参数

    前台发送:&warning_type[]=1,2 &warning_type=1,2 后台接收:(@RequestParam(value = "param[]") ...

  2. JVM--Java类加载机制

    一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其存放在运行时数据区的方法区内,然后在java堆区创建一个java.lang.Class对象,用来封装类在方法区内 ...

  3. 数据库Mysql的学习(三)-各种约束

    删除数据库表 drop table [if exists] 表一,表二.....; 表分区:比如图书信息表有1000万个图书信息,如何优化他,其中一种方式就是表分区.就是把一张表的数据分成多个区块,这 ...

  4. java核心技术 笔记

    一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...

  5. Training Models

    In this page, I am going to talk about the 'hello world' model that is linear regression and train i ...

  6. Solium代码测试框架

    Solium, 在solid中,Linter用于标识和修复样式&安全问题 //调用测试 solium -d contracts --fix 源代码名称:Solium 源代码网址:http:// ...

  7. 以太坊开发(二)使用Ganache CLI在私有链上搭建智能合约

    以太坊开发(二)使用Ganache CLI在私有链上搭建智能合约 在上一篇文章中,我们使用Truffle自带的客户端Truffle Develop,在私有链上搭建并运行了官方提供的WebPack智能合 ...

  8. spring boot 中文乱码问题

    在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...

  9. UVALive 3668 A Funny Stone Game(博弈)

    Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,...,  ...

  10. js如何处理字符串中带有↵字符

    js或vue中如何处理字符串中带有↵字符 split('\n') 使用split('\n')将字符串分割成数组就行 如果我们在vue中,只需要在页面中绑定变量时操作split('\n')就可以了: & ...