zoukankan      html  css  js  c++  java
  • uva10066 The Twin Towers(dp 最长公共子序列)

    大意就是给出两排数字, 求最大公共子序列。

    题目:

    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

    代码:

     1 #include <iostream>
     2 #include <memory.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int tow1[105];
     7 int tow2[105];
     8 int d[105][105];
     9 int main()
    10 {
    11     int N1,N2;
    12     int tst=0;
    13 
    14     while(cin>>N1>>N2&& N1!=0 && N2!=0)
    15     {
    16         for(int i=0;i<N1;i++)
    17             cin>>tow1[i];
    18 
    19         for(int i=0;i<N2;i++)
    20             cin>>tow2[i];
    21 
    22 
    23         for(int i=1; i<=N1;i++)
    24         {
    25             for(int j=1;j<=N2;j++)
    26             {
    27                 if( tow1[i-1] == tow2[j-1] )
    28                 {
    29                     d[i][j]= d[i-1][j-1]+1;
    30                 }
    31                 else
    32                 {
    33                     d[i][j] = max ( d[i-1][j],d[i][j-1]);
    34                 }
    35             }
    36         }
    37         cout<<"Twin Towers #"<<++tst<<endl;
    38         cout<<"Number of Tiles : "<<d[N1][N2]<<endl;
    39 
    40         cout<<endl;
    41 
    42         memset(d,0,sizeof(d));
    43 
    44     }
    45 
    46     return 0;
    47 }
  • 相关阅读:
    java实现HTTP请求 HttpUtil
    java-websocket客户端 断线重连 注入Service问题
    人工智能博客
    git 修改注释
    2019-2-22
    2019-2-21
    2019-2-20
    /与./和../的含义
    第二章(构建有多个房间的聊天室程序)
    第一章(欢迎进入node.js世界)
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3496647.html
Copyright © 2011-2022 走看看