UVa 10066 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

 

基本动态规划问题——最长公共子序列问题

设dp[i][j]表示A1A2…Ai和B1B2…Bj两个子串的最长公共子序列的长度

状态转移方程为:

  若A[i]==B[j],dp[i][j]=dp[i-1][j-1]+1

  否则,dp[i][j]=max(dp[i-1][j],dp[i][j-1])

 

UVa 10066 The Twin Towers
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int n1,n2;
 8 int r1[150],r2[150],dp[150][150];
 9 
10 int Max(int a,int b,int c)
11 {
12     return a>b?(a>c?a:c):(b>c?b:c);
13 }
14 
15 int main()
16 {
17     int kase=0;
18 
19     while(scanf("%d %d",&n1,&n2)==2&&(n1||n2))
20     {
21         kase++;
22         memset(dp,0,sizeof(dp));
23         printf("Twin Towers #%d\nNumber of Tiles : ",kase);
24 
25         for(int i=0;i<n1;i++)
26             scanf("%d",&r1[i]);
27         for(int i=0;i<n2;i++)
28             scanf("%d",&r2[i]);
29 
30         bool flag=false;
31         for(int i=0;i<n1;i++)
32             if(flag||r1[i]==r2[0])
33             {
34                 flag=true;
35                 dp[i][0]=1;
36             }
37         flag=false;
38         for(int j=0;j<n2;j++)
39             if(flag||r1[0]==r2[j])
40             {
41                 flag=true;
42                 dp[0][j]=1;
43             }
44 
45         for(int i=1;i<n1;i++)
46             for(int j=1;j<n2;j++)
47             {
48                 if(r1[i]==r2[j])
49                     dp[i][j]=dp[i-1][j-1]+1;
50                 dp[i][j]=Max(dp[i][j],dp[i-1][j],dp[i][j-1]);
51             }
52 
53         printf("%d\n\n",dp[n1-1][n2-1]);
54     }
55 
56     return 0;
57 }
[C++]

UVa 10066 The Twin Towers

上一篇:[转]Windows 7 蓝屏后获取 MEMORY.DMP 文件及注意事项


下一篇:linux救援模式下挂载lvm分区