uva 10192 Vacation

The Problem

You are planning to take some rest and to go out on vacation, but you really don’t know which cities you should visit. So, you ask your parents for help. Your mother says “My son, you MUST visit Paris, Madrid, Lisboa and London. But it’s only fun in this order.” Then your father says: “Son, if you’re planning to travel, go first to Paris, then to Lisboa, then to London and then, at last, go to Madrid. I know what I’m talking about.”

Now you’re a bit confused, as you didn’t expected this situation. You’re afraid that you’ll hurt your mother if you follow your father’s suggestion. But you’re also afraid to hurt your father if you follow you mother’s suggestion. But it can get worse, because you can hurt both of them if you simply ignore their suggestions!

Thus, you decide that you’ll try to follow their suggestions in the better way that you can. So, you realize that the “Paris-Lisboa-London” order is the one which better satisfies both your mother and your father. Afterwards you can say that you could not visit Madrid, even though you would’ve liked it very much.

If your father have suggested the “London-Paris-Lisboa-Madrid” order, then you would have two orders, “Paris-Lisboa” and “Paris-Madrid”, that would better satisfy both of your parent’s suggestions. In this case, you could only visit 2 cities.

You want to avoid problems like this one in the future. And what if their travel suggestions were bigger? Probably you would not find the better way very easy. So, you decided to write a program to help you in this task. You’ll represent each city by one character, using uppercase letters, lowercase letters, digits and the space. Thus, you can have at most 63 different cities to visit. But it’s possible that you’ll visit some city more than once.

If you represent Paris with “a”, Madrid with “b”, Lisboa with “c” and London with “d”, then your mother’s suggestion would be “abcd” and you father’s suggestion would be “acdb” (or “dacb”, in the second example).

The program will read two travel sequences and it must answer how many cities you can travel to such that you’ll satisfy both of your parents and it’s maximum.

The Input

The input will consist on an arbitrary number of city sequence pairs. The end of input occurs when the first sequence starts with an “#”character (without the quotes). Your program should not process this case. Each travel sequence will be on a line alone and will be formed by legal characters (as defined above). All travel sequences will appear in a single line and will have at most 100 cities.

The Output

For each sequence pair, you must print the following message in a line alone:

Case #d: you can visit at most K cities.

Where d stands for the test case number (starting from 1) and K is the maximum number of cities you can visit such that you’ll satisfy both you father’s suggestion and you mother’s suggestion.

Sample Input

abcd

acdb

abcd

dacb

#

Sample Output

Case #1: you can visit at most 3 cities.

Case #2: you can visit at most 2 cities.

题目大意:最长公共子序列。

解题思路:最长公共子序列。注意要用gets来读。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define N 150
using namespace std;
char a[N], b[N];
int dp[N][N];
int main() {
int Case = 1;
while (gets(a) != NULL) {
if (a[0] == '#') break;
gets(b);
memset(dp, 0, sizeof(dp));
int l1 = strlen(a), l2 = strlen(b);
int Max = 0;
for (int i = 1; i <= l1; i++) {
for (int j = 1; j <= l2; j++) {
if (a[i - 1] == b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
printf("Case #%d: you can visit at most %d cities.\n", Case++, dp[l1][l2]);
}
return 0;
}

版权声明:本文博主原创文章。博客不能未经同意转载。

uva 10192 Vacation(最长公共子)的更多相关文章

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

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  2. 使用后缀数组寻找最长公共子字符串JavaScript版

    后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...

  3. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  4. UVa 10192 - Vacation &amp; UVa 10066 The Twin Towers ( LCS 最长公共子串)

    链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...

  5. LIS(最长的序列)和LCS(最长公共子)总结

    LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...

  6. UVA 10192 Vacation

    裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...

  7. POJ 3356 AGTC(最长公共子)

    AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform  ...

  8. KMP该算法解释(最长公共子)

    一个:介绍KMP算法之前,首先解释一下BF算法 (1)BF算法(传统的匹配算法,是最简单的算法) BF算法是一种常见的模式匹配算法,BF该算法的思想是目标字符串S模式串的第一个字符P的第一个字符,以匹 ...

  9. POJ 2774 后缀数组:查找最长公共子

    思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...

随机推荐

  1. Mysql iot表

    我们知道一般的表都以堆(heap)的形式来组织的,这是无序的组织方式. Oracle还提供了一种有序的表,它就是索引组织表,简称IOT表.IOT表上必须要有主键,而IOT表本身不对应segment,表 ...

  2. linux下的块设备驱动(二)

    上一章主要讲了请求队列的一系列问题.下面主要说一下请求函数.首先来说一下硬盘类块设备的请求函数. 请求函数可以在没有完成请求队列的中的所有请求的情况下就返回,也可以在一个请求都不完成的情况下就返回. ...

  3. 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!

    其实很简单: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, D ...

  4. [Windows Phone学习笔记]页面之间传递对象

    在Windows Phone中,页面之间传递参数就类似Web开发中一样,通过QueryString的形式进行传递,但是如果需要传递对象,则无法通过QueryString形式了,其实也可以,把对象序列化 ...

  5. [Android]Eclipse的使用

    1.取消Eclipse拼写检查 General -> Editors -> Text Editors -> Spelling 取消enable spell checking 前面的勾 ...

  6. Android获得Manifest在&lt;meta-data&gt;元件的值

    前段时间攻略完成游戏开发项目.其中用于包装散装. 目前市场上的网络不提交.但是,通过设置Manifest中的Meta_data>去获得相关參数,游戏ID号改变.游戏ID改变,然后游戏内容就改变. ...

  7. No-Touch Integration 在SharePoint中使用社区支持的Silverlight应用程序

    No-Touch Integration 在SharePoint中使用社区支持的Silverlight应用程序         No-Touch Integration应该是最简单的方法了.将Silv ...

  8. struts2文件上传限制大小问题

    struts2默认文件上传大小为2M,如需改动默认大小,解决方法例如以下: <struts> <constant name="struts.multipart.maxSiz ...

  9. linux LVS DR模式配置

    拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1.  安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...

  10. libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET

    libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET libevent安装总结 分类: linux 系统配置 2013-02-13 22:37 99人阅读 评论( ...