博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 10066 The Twin Towers(LCS)
阅读量:6880 次
发布时间:2019-06-27

本文共 3012 字,大约阅读时间需要 10 分钟。

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

 

1 # include
2 # include
3 # include
4 using namespace std; 5 int N1,N2; 6 int s1[105],s2[105]; 7 int dp[105][105]; 8 int main() 9 {10 int cas=1;11 int i,j;12 while(scanf("%d%d",&N1,&N2)&&N1&&N2)13 {14 for(i=1; i<=N1; i++)15 scanf("%d",&s1[i]);16 for(i=1; i<=N2; i++)17 scanf("%d",&s2[i]);18 printf("Twin Towers #%d\n",cas++);19 20 memset(dp,0,sizeof(dp)); //边界条件21 int ans=0;22 for(i=1; i<=N1; i++) //状态转移23 for(j=1; j<=N2; j++)24 {25 if(s1[i]==s2[j])26 dp[i][j] = dp[i-1][j-1]+1;27 else28 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);29 if(ans

 

 

转载地址:http://lgubl.baihongyu.com/

你可能感兴趣的文章
09-排序2 Insert or Merge
查看>>
矩阵与线性方程组
查看>>
第六天
查看>>
linux杀毒软件clamav安装与使用
查看>>
安装ubuntu系统 ——分区
查看>>
oracle学习_基本语法
查看>>
【Todo】STAR面试法
查看>>
信号量与条件变量的区别
查看>>
thinkphp3.1课程 1-1 为什么thinkphp在开发好后需要关掉开发模式
查看>>
Teradata 语句简单优化
查看>>
c# 通过关键字查询
查看>>
已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数...
查看>>
jquery
查看>>
伏地魔
查看>>
linux
查看>>
安装虚拟机-linux系统步骤
查看>>
集训第五周动态规划 J题 括号匹配
查看>>
微信小程序车牌键盘
查看>>
python 网络编程
查看>>
【BZOJ】2165: 大楼
查看>>