两个塔,由一些圆盘组成,现在要从两个塔去掉一些圆盘,使得两个塔完全一样,求出最高的塔高,也就是最长的公共自序列.注意输出格式.
/*************************************************************************
> File Name: 10066.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年11月12日 星期三 21时10分26秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
#define N 110
int dp[N][N], num1[N], num2[N];
/*
* 最长公共子序列,注意输出格式
*/
int main(int argc, char *argv[])
{
int c = 0;
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
if (n == 0 && m == 0) {
break;
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &num1[i]);
}
for (int i = 1; i <= m; ++i) {
scanf("%d", &num2[i]);
}
clr(dp, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (num1[i] == num2[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
printf("Twin Towers #%d\n", ++c);
printf("Number of Tiles : %d\n\n", dp[n][m]);
}
return 0;
}
/*
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
*/
代码如下:

本文探讨了如何通过最长公共子序列算法解决两个不同高度的塔(由圆盘组成)的问题,目标是使两个塔的高度尽可能相同。通过输入塔的高度信息,输出最高可能的共同塔高度。
760

被折叠的 条评论
为什么被折叠?



