uva 10192 Vacation(最长公共子)
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(最长公共子)的更多相关文章
- uva 10066 The Twin Towers (最长公共子)
uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...
- 使用后缀数组寻找最长公共子字符串JavaScript版
后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...
- LIS(最长的序列)和LCS(最长公共子)总结
LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1 ...
- UVA 10192 Vacation
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- POJ 3356 AGTC(最长公共子)
AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform ...
- KMP该算法解释(最长公共子)
一个:介绍KMP算法之前,首先解释一下BF算法 (1)BF算法(传统的匹配算法,是最简单的算法) BF算法是一种常见的模式匹配算法,BF该算法的思想是目标字符串S模式串的第一个字符P的第一个字符,以匹 ...
- POJ 2774 后缀数组:查找最长公共子
思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...
随机推荐
- ThinkPHP使用分组详细介绍(十七)
原文:ThinkPHP使用分组详细介绍(十七) 使用分组(模块分组) *就是将多个项目合并到一个项目/应用去(就是Home.Admin) ---分组不分组看自己的建立项目习惯,个人习惯用根目录配置生成 ...
- 使用HashMap须要注意的事儿:不要暴露Map.entry给外部不可信代码Map.entrySet()
Map/HashMap是java中一种非经常常使用的数据结构,一般我们在应用中做的事情就是调用put向容器写入数据或者是get从容器读取数据. Map.entrySet()这种方法返回了键值对的集合, ...
- 扯谈网络编程之自己实现ping
ping是基于ICMP(Internet Control Message Protocol)协议实现的.而ICMP协议是在IP层实现的. ping实际上是发起者发送一个Echo Request(typ ...
- ExtJS学习--------Ext.Element中的经常使用事件和其它重要的方法学习(实例)
经常使用事件: 其它重要方法: 详细实例:(实例结果能够将相应的代码取消凝视进行測试) Ext.onReady(function(){ Ext.create('Ext.panel.Panel',{ t ...
- oracle 通过查询灵活插入数据 insert into ...select..
insert into reg_user (id,name,password,area_code,reg_time,first_pswd,record_type) select l.reg_user_ ...
- Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02
/* 场景一lua代码 */ require "MainScene02" local dic_size = CCDirector:sharedDirector():getWinSi ...
- SCU 3133(博弈)
传送门:windy和水星 -- 水星游戏 2 题意:在一张由 n*m 的格子组成的棋盘上放着 k 个骑士每个骑士的位置为(xi,yi),表示第xi行,第yi列骑士如果当前位置为(x,y),一步可以走的 ...
- tolua 有些功能可以用(经过测试)
tolua 提供几个 C++ 与 Lua 进行数据交换的工具函数. ~~ tolua.type 返回一个 C++ 对象的类型描写叙述字符串. local node = display.newNode( ...
- 【转】Vim学习资料
初学资料:1:一个介绍VIM操作的游戏,十分适合初学者.只是:不要怕英文.vim-adventures.com2:http://blog.csdn.net/niushuai666/article/de ...
- Java集群--大型网站是怎样解决多用户高并发访问的
时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...