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.

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.

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.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

简单的最长子序列匹配,算法没什么难度,但是想要ac还需要注意的是程序的输入和输出,不要因为题目简单而在最简单的输入输出上栽跟头,本题中输入字符串c++必须使用getline(cin,str),否则ac就会失败

ac代码如下:

#include <iostream>
#include <cstring> using namespace std;
int d[110][110];
int main()
{
string a,b;
int i,j;
int count=0;
while(getline(cin,a)) //必须使用getline 才能ac,cin输入字符串将会错误
{
if(a=="#")
{
break;
}
getline(cin,b);
memset(d,0,sizeof(d));
for(i=1; i<=a.size(); i++)
for(j=1; j<=b.size(); j++)
{
if(a[i-1] == b[j-1])
{
d[i][j] = d[i-1][j-1]+1;
}
else
{
d[i][j] = max(d[i-1][j],d[i][j-1]);
}
}
cout << "Case #"<<++count<<": you can visit at most "<<d[a.size()][b.size()]<<" cities." << endl;
a.clear();
b.clear();
}
return 0;
}

ACM练习中关于LCS的题目的更多相关文章

  1. 浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法

    浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法 众所周知,把[0,1]区间内的n个随机.相互独立的实数变量\(x_i\)之间的大小关系写成一个排列\(\{p_i\}\),使得\(\f ...

  2. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  3. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

  4. iOS面试中常见的算法题目

    一.前言 这里是在iOS求职中自己遇到的算法题,希望对大家有所帮助.不定期更新.如果大家想在线运行代码调试,可以将代码拷贝到这里.然后进行调试.下面就是常见的算法题目. 二.正文 1.就n的阶乘.(这 ...

  5. PTA中如何出Java题目?

    PTA中如何出Java题目? 很多第一次出题的老师,不知道Java在PTA中是如何处理输入的.写一篇文章供大家参考.比如以下这样的一个题目: 从控制台读入两个数,然后将其相加输出. 对于该题可以有如下 ...

  6. Java面试题整理:这些Java程序员面试中经常遇见的题目,必须掌握才能有好结果

    1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用时 ...

  7. java中有关线程的题目

    1,看一下下面程序错误发生在哪一行! class Test implements Runnable{ public void run(Thread t){ } } 2,输出结果是什么? class T ...

  8. JS学习中遇到的一些题目

    1.找出所有的水仙花数: 水仙花数例如:153 的特点: 1^3+5^3+3^=153 而且水仙花数只会是三位数,所以可以利用循环的方式来解决问题,循环条件可以设为: var i = 1;i < ...

  9. JavaScript中对象数组 作业题目以及作业

    var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, gender, age, salary) { t ...

随机推荐

  1. 初学python之路-day01

    第一天学习python,先了解到了进制之间的转换关系. 如二进制与十进制的转换,如1111转成十进制为15,1111从左向右可看出2^3+2^2+2^1+2^0为8+4+2+1=15.记住前8位1的二 ...

  2. ES6 语法学习(二)

    1.类的建立与继承 constructor方法是类的构造函数是默认方法,通过new命令生成对象实例时,自动调用该方法.一个类必须有constructor方法,如果没有显式定义,一个默认的constru ...

  3. iOS关键词weak和assign的区别

    一.区别 首先说说在什么情况下使用 weak 关键字 1.ARC中,在有可能出现循环引用的时候,往往要通过让其中一端使用weak来解决,比如:delegate 的代理属性. 2.自身已经对它有过一次强 ...

  4. 如何在cocos中为节点添加监听事件

    一般在监听键盘事件时,可是采用以下方式来监听键盘事件: 以及记得定义取消监听的函数(这个摧毁函数会自己调用吗?): 同时这里还有一种传统的监听方式: 但是cocos官方的文档建议我们不要使用这种方式, ...

  5. Grafana和influxdb监控nginx日志中的请求响应时间图形化监控

    监控效果如图: 监控方法: 通过logstash过滤nginx日志,然后解析出nginx日志中的request time字段 然后output到influxdb时序数据库中 通过grafana展示数据 ...

  6. net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建

    系统要求 首先建议采用 Windows 10 专业版/企业版/教育版,且必须是64位操作系统,原因是docker装起来比较方便,Win7装起来比较麻烦,且不确定是否有其他问题(自己没有实践过) 其次W ...

  7. Fibonacci数列(数列 取模)

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...

  8. 关于UITabBarController的设置(iOS 开发)

    1.设置图片(选中以及未选中) UITabBarItem *TuiJianItem=[[UITabBarItem alloc]initWithTitle:@"我的" image:[ ...

  9. asp.net MVC jsonp跨域获取数据

    public class JsonpResult : JsonResult { object _data = null; public JsonpResult() { } public JsonpRe ...

  10. 11. cookie_session_原生ajax_readyState的值_同源策略_跨域_jsonp的使用

    1. cookie 浏览器存储技术.(服务器将少量数据交于浏览器存储管理) 作用: 存储数据, 解决 http 协议无状态问题 工作流程: 浏览器发送请求给服务器,请求登录 服务器返回响应给浏览器,此 ...