UVA 10066 The Twin Towers(LCS)
Problem B
The Twin Towers
Input: standard input
Output: standard output
Once upon a time, in an ancient Empire, there were two towers of dissimilar shapes in two different cities. The towers were built by putting circular tiles one upon another. Each of the tiles was of the same height and had integral radius. It is no wonder that though the two towers were of dissimilar shape, they had many tiles in common.
However, more than thousand years after they were built, the Emperor ordered his architects to remove some of the tiles from the two towers so that they have exactly the same shape and size, and at the same time remain as high as possible. The order of the tiles in the new towers must remain the same as they were in the original towers. The Emperor thought that, in this way the two towers might be able to stand as the symbol of harmony and equality between the two cities. He decided to name them the Twin Towers.
Now, about two thousand years later, you are challenged with an even simpler problem: given the descriptions of two dissimilar towers you are asked only to find out the number of tiles in the highest twin towers that can be built from them.
Input
The input file consists of several data blocks. Each data block describes a pair of towers.
The first line of a data block contains two integers N1 and N2 (1 <= N1, N2 <= 100) indicating the number of tiles respectively in the two towers. The next line contains N1 positive integers giving the radii of the tiles (from top to bottom) in the first tower. Then follows another line containing N2 integers giving the radii of the tiles (from top to bottom) in the second tower.
The input file terminates with two zeros for N1 and N2.
Output
For each pair of towers in the input first output the twin tower number followed by the number of tiles (in one tower) in the highest possible twin towers that can be built from them. Print a blank line after the output of each data set.
Sample Input
7 6
20 15 10 15 25 20 15
15 25 10 20 15 20
8 9
10 20 20 10 20 10 20 10
20 10 20 10 10 20 10 10 20
0 0
Sample Output
Twin Towers #1
Number of Tiles : 4
Twin Towers #2
Number of Tiles : 6
题目大意:给定构成两座塔瓦片,从中挑选出一些瓦片留下,使得两座塔有相同的高度和结构,成为具有最多数量瓦片的双胞胎塔。
动态规划LCS
# include<cstdio>
# include<cstring>
# include<iostream>
using namespace std;
int N1,N2;
int s1[],s2[];
int dp[][];
int main()
{
int cas=;
int i,j;
while(scanf("%d%d",&N1,&N2)&&N1&&N2)
{
for(i=; i<=N1; i++)
scanf("%d",&s1[i]);
for(i=; i<=N2; i++)
scanf("%d",&s2[i]);
printf("Twin Towers #%d\n",cas++); memset(dp,,sizeof(dp)); //边界条件
int ans=;
for(i=; i<=N1; i++) //状态转移
for(j=; j<=N2; j++)
{
if(s1[i]==s2[j])
dp[i][j] = dp[i-][j-]+;
else
dp[i][j] = max(dp[i-][j],dp[i][j-]);
if(ans<dp[i][j])
ans = dp[i][j];
}
printf("Number of Tiles : %d\n\n",ans);
}
return ;
}
UVA 10066 The Twin Towers(LCS)的更多相关文章
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- 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> ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- UVA 10066 The Twin Towers
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- 【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)
昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以 ...
- 动态规划法(十)最长公共子序列(LCS)问题
问题介绍 给定一个序列\(X=<x_1,x_2,....,x_m>\),另一个序列\(Z=<z_1,z_2,....,z_k>\)满足如下条件时称为X的子序列:存在一个严格 ...
- uva 1153 顾客是上帝(贪心)
uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...
- 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)
最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...
随机推荐
- 【解决】UEFI+GPT模式下装系统(WIN7/WIN8)
最近在家想把联想超极本重装系统,可是发现想简单了,预装WIN8的本本用的是UEFI+GPT模式,以前老毛桃装系统那一套不好用了,所以百度了一些方案,还没试,先记着. 1. WIN8 先说装WIN8,貌 ...
- boost------function的使用(Boost程序库完全开发指南)读书笔记
function是一个函数对象的“容器”,概念上像是c/c++中函数指针类型的泛化,是一种“智能函数指针”.它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象. 因此, ...
- oc学习之路----代理模式2-使用步骤
之前已经写过一个个人关于代理模式的一些看法,现在就来总结一下使用代理模式的步骤吧. 1.先搞清楚谁是谁的代理(delegate) ● 2.定义代理协议,协议名称的命名规范:控件类名 + Delegat ...
- 使用sqlite3 有关tableview删除cell的问题
在root页面,想要删除tableviewcell,是有一定顺序的 首先要删除 数据库sqlite3 中的数据,然后删除数组中的数据,最后删除cell 一般我们知道,删除cell要在删除数组数据之后, ...
- IOS tableView 自定义cell 多行时 文字重复现象
今天写了一个demo,有一个问题,有很多cell,但是超过一页后往下翻,发现文字有重叠现象, cell用的是重用机制,按说不会这样,最终解决的方案: 勾选这个,clears graphics cont ...
- excel导入数据库iis设置
导入成功以后,基本这个小项目的所有功能都开发完成了,请IT部门帮我设定了一个固定IP,我以本机作为服务器,在本机IIS上发布了一个测试版,结果上传Excel数据报错, 错误信息“未在本地计算机上注册“ ...
- [Whole Web, Node.js PM2] Loggin with PM2
Add config for app's log and error log for PM2. { "apps": [{ "name": "App1& ...
- crash recovery
2016-07-02 17:56:07 5772 [Note] InnoDB: Database was not shutdown normally!2016-07-02 17:56:07 5772 ...
- careercup-树与图 4.1
4.1 实现一个函数,检查二叉树是否平衡.在这个问题中,平衡树的定义如下:任意一个结点,其两颗子树的高度差不超过1. C++实现代码: #include<iostream> #includ ...
- 写个 Hello World 而已,要不要这么震撼?!
Atom 编辑器的一个插件,可以让你写代码的时候体验狂拽酷炫的效果! 如果来点音乐.再配上机械键盘的话,写代码是不是爽到爆呢? 这货全名叫: activate-power-mode atom pack ...