10066 - The Twin Towers
两个塔,由一些圆盘组成,现在要从两个塔去掉一些圆盘,使得两个塔完全一样,求出最高的塔高,也就是最长的公共自序列.注意输出格式.
/*************************************************************************> 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 110int 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 outputOnce 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 0Sample Output
Twin Towers #1
Number of Tiles : 4Twin Towers #2
Number of Tiles : 6
*/

代码如下:

uvaoj 10066 - The Twin Towers 最长公共子序列(LCS)相关推荐

  1. 动态规划算法解最长公共子序列LCS问题

    动态规划算法解LCS问题 作者 July 二零一零年十二月三十一日 本文参考:微软面试100题系列V0.1版第19.56题.算法导论.维基百科. 第一部分.什么是动态规划算法 ok,咱们先来了解下什么 ...

  2. 动态规划之最长公共子序列(LCS)

    最长公共子序列(LCS,Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最 ...

  3. 程序员编程艺术第十一章:最长公共子序列(LCS)问题

    程序员编程艺术第十一章:最长公共子序列(LCS)问题 0.前言 程序员编程艺术系列重新开始创作了(前十章,请参考程序员编程艺术第一~十章集锦与总结).回顾之前的前十章,有些代码是值得商榷的,因当时的代 ...

  4. 算法之最长公共子序列(LCS)问题

    算法课上老师留的作业,最长公共子序列LCS(Longest Common Subsequence)问题,首先看到这个问题感觉有点复杂,和最长公共子串不同,公共子序列并不要求元素相邻,看起来只有穷举才能 ...

  5. 最长公共子序列php,动态规划(最长公共子序列LCS)

    概念 求解决策过程最优化的结果 (可能有多个) 把多阶段过程转化为一系列单阶段过程,利用各阶段之间的关系,逐个求解 计算过程中会把结果都记录下,最终结果在记录中找到. 举例 求两个字符串的最长公共子序 ...

  6. python实现求解最长公共子序列LCS问题

    在实现论文<Automatically Generating Models for Botnet Detection>论文的算法中,用到了一个The longest commom subs ...

  7. 算法导论-----最长公共子序列LCS(动态规划)

    目录 一.概念梳理 二.最长公共子序列解决方案 方案1:蛮力搜索策略 方案2:动态规划策略 三.C代码实现 实现1 实现2(空间优化) 一.概念梳理   1. 子序列(subsequence): 一个 ...

  8. 动态规划表格法解决最长公共子序列(LCS)问题

    3.5 最长公共子序列(LCS) 前言:图片是博主自己画的,转载请注明出处哦 3.5.1 问题描述 最长公共子序列(Longest Common Subseuence,LCS)问题:给定两个字符串,求 ...

  9. 最长公共子序列 (LCS) 详解+例题模板(全)

    欢迎访问https://blog.csdn.net/lxt_Lucia-- 宇宙第一小仙女\(^o^)/-萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗- ------------ ...

  10. 相似度:最长公共子序列--LCS

    一.概念 1.子序列 一个特定序列的子序列就是将给定序列中零个或多个元素去掉后得到的结果(不改变元素间相对次序).如序列[A,B,C,B,D,A,B]的子序列有:[A,B],[B,C,A],[A,D, ...

最新文章

  1. C# tips ---值类型的装箱和拆箱
  2. HTML5--sessionStorage、localStorage、manifest
  3. 【转载】手把手教你配置Windows2003集群(图)
  4. 概率论-4.2中心极限定理(待补充)
  5. java环境变量path好长_java环境变量设置
  6. 10分钟搭建商品结算平台!商品、车辆识别一网打尽
  7. libcareplus支持的补丁类型
  8. 一个新的自己从2009年的第一天...
  9. 【动态规划】01背包问题:购物袋
  10. debian配置JDK环境变量
  11. Photoshop插件-加深减淡中灰图-脚本开发-PS插件
  12. sqlserver200864位下载_microsoft sql server 2008官方下载|Microsoft SQL Server 200832/64位 完整版_ - 极光下载站...
  13. GPUImage学习日记(4)之添加文字水印
  14. Vue项目 在chrome页面崩溃:喔唷 崩溃了(总结)
  15. Markov blanket 马尔科夫毯子
  16. mysql 日志重做,設置MySQL重做日志大小
  17. PS钢笔抠图及商业案例
  18. 第二课《shell 编程基础》
  19. camel动态设置路由执行
  20. 数字电路基础知识——组合逻辑电路之乘法器的设计(一)—— 并行、移位相加、加法树、查找表乘法器

热门文章

  1. google谷歌云盘_如何在酒店房间使用Google Chromecast?
  2. 2022牛客寒假训练营1-K冒险公社
  3. adobe 免费素材库
  4. 关于:使用 OCT 自定义部署 Office 2007-2016
  5. 硬盘文件系统系列专题之二 NTFS
  6. 优秀的程序员都热爱写作
  7. LTE物理层概述(7)-- LTE之Turbo编码及其matlab仿真1
  8. python之js破解qq邮箱登录
  9. IDEA报错:Error:java: JDK isn‘t specified for module ‘work-flow‘
  10. 我曾经学习过的地方--中国欧盟可用性研究中心