[动态规划]UVA10066 - The Twin Towers

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

____________________________________________________________________________________
Rezaul Alam Chowdhury

题意:

从前从前在古帝国有2座高塔位于2座城市中,他们的形状不太相同。但是他们都是用圆柱形的石块一个堆在另一个上面建起来的。每个圆柱形石块的高度都相同(定为1),但是半径却不一。所以,虽然2座高塔的形状不一样,但事实上他们可能有许多石块是相同的。

在高塔建成的一千年后,国王要求建筑师拿掉高塔的某些石块,使得2座高塔的形状大小和高度一样。但同时要尽可​​能让高塔的高度越高越好。新高塔的石块的顺序也必须和原来的高塔一样。国王认为这样可以代表2座城市之间的和谐与平等。他为这2座高塔命名为「双子星塔」

现在,你的任务是就是算出这双子星塔的高度。

思路:典型最长匹配子串问题,找到状态转移公式就可以了。

#include<iostream>
#include<cstring>

using namespace std;

int dp[110][110];
int arry1[110],arry2[110];

int main()
    {
        int len1,len2;
        int pos=0;
        while(cin>>len1>>len2&&(len1!=0||len2!=0))
            {
                int i,j,k;
                for(i=0;i<len1;i++)
                    cin>>arry1[i];
                for(i=0;i<len2;i++)
                    cin>>arry2[i];
                memset(dp,0,sizeof(dp));
                for(i=1;i<=len1;i++)
                    {
                        for(j=1;j<=len2;j++)
                            {
                                if(arry1[i-1]==arry2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
                                else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
                            }
                    }
                cout<<"Twin Towers #"<<++pos<<endl;
                cout<<"Number of Tiles : "<<dp[len1][len2]<<endl;
                cout<<endl;
            }
        return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值