UVa 10066 The Twin Towers(最长公共子序列)

The Twin Towers

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Description

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<cstdio>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=200+10;
 6 int a[maxn],b[maxn];
 7 int dp[maxn][maxn]={0};
 8
 9 int main()
10 {
11      int n,m;
12      int cas=0;
13      while(scanf("%d%d",&n,&m)!=EOF && n+m)
14      {
15           cas++;
16           for (int i=1;i<=n;i++)
17             scanf("%d",&a[i]);
18           for (int i=1;i<=m;i++)
19             scanf("%d",&b[i]);
20           for (int i=1;i<=n;i++)
21           {
22                for (int j=1;j<=m;j++)
23                {
24                     if (a[i]==b[j])
25                         dp[i][j]=dp[i-1][j-1]+1;
26                     else
27                         dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
28                }
29           }
30           printf("Twin Towers #%d\n",cas);
31           printf("Number of Tiles : %d\n\n",dp[n][m]);
32      }
33      return 0;
34 }
时间: 2024-10-22 00:05:04

UVa 10066 The Twin Towers(最长公共子序列)的相关文章

uva 10066 The Twin Towers (最长公共子)

uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(&quo

uva 10066 The Twin Towers (最长公共子序列)

uva 10066 The Twin Towers 题目大意:最长公共子序列. 解题思路:最长公共子序列. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

UVA 10635 Prince and Princess 最长公共子序列(nlongn)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19051 题目意思是给你两串数字(计为 a,b 数组),让你求他们的最长公共子序列.数字长度是 n * n (n <= 250)所以 O(n^2) 肯定过不了了.所以想要找 O(nlongn)的. 因为题目给出了数字在 1 ~ (n^2)内并且数字不会重复.因此,对于a数组中的每个数字,如果我们都知道他在b数组中出先得位置的话(位置计为 c 数组),我们只需要在c数组里面求一

UVA 10066 The Twin Towers

DP,题目很长,题意就是求LCS. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<

UVa 10066 Twin Towers (DP 最长公共子序列)

题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=105; int a[maxn],b[maxn],d[maxn][maxn],na,nb; void lcs() { memset(d,0,sizeof(d)); for(int i=1; i<=na; ++i) for(in

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a