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

  1. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  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. LightOJ1126 Building Twin Towers(DP)

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

  5. UVA 10066 The Twin Towers

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

  6. 【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)

        昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以 ...

  7. 动态规划法(十)最长公共子序列(LCS)问题

    问题介绍   给定一个序列\(X=<x_1,x_2,....,x_m>\),另一个序列\(Z=<z_1,z_2,....,z_k>\)满足如下条件时称为X的子序列:存在一个严格 ...

  8. uva 1153 顾客是上帝(贪心)

    uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...

  9. 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

    最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

随机推荐

  1. 问题-Delphi记忆工程打开的单元(XE2设置项)

    问题情况:每次在delphi中打开了N个单元的窗口,关闭delphi后,第二天还得一个一个打开单元窗口.问题原因:这是因为delphi的记忆功能未打开.问题处理:Tools->Options.. ...

  2. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  3. bootstrap多层模态窗

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  4. 关于JAVA中URL传递中文参数的问题

    今天在调用一个接口时,需要传递一个中文参数,结果获取不到数据,原因就在于中文传参的编码问题. 问题来源:URL url= new URL("http://XXX?OrganName=司法厅& ...

  5. Filter案例

    1.有选择的被访问 描述:首先若用户没有在页面提交注册(直接访问list.jsp),就只能被允许访问a.jsp.其他页面均不被允许访问 在login.jsp提交信息之后,可以在b.jsp访问, 代码如 ...

  6. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

  7. 横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式

    转载:http://blog.csdn.net/nogodoss/article/details/17246489 最近搞横竖屏,获得一些心得,特记录下来. 做横竖屏最重要的是确定横竖屏响应的接口.目 ...

  8. jrae源代码解析(二)

    本文细述上文引出的RAECost和SoftmaxCost两个类. SoftmaxCost 我们已经知道.SoftmaxCost类在给定features和label的情况下(超參数给定),衡量给定权重( ...

  9. DelphiTXT文档编辑器

    EXE:http://files.cnblogs.com/xe2011/Text_EditorRelease2013-12-20-185320.rar Delphi XE5 PAS:http://fi ...

  10. java缓存算法【转】

    http://my.oschina.net/u/866190/blog/188712 提到缓存,不得不提就是缓存算法(淘汰算法),常见算法有LRU.LFU和FIFO等算法,每种算法各有各的优势和缺点及 ...