ACM练习中关于LCS的题目
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的题目的更多相关文章
- 浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法
浅谈[0,1]区间内的n个随机实数变量中增加偏序关系类题目的解法 众所周知,把[0,1]区间内的n个随机.相互独立的实数变量\(x_i\)之间的大小关系写成一个排列\(\{p_i\}\),使得\(\f ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- ACM 计算几何中的精度问题(转)
http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...
- iOS面试中常见的算法题目
一.前言 这里是在iOS求职中自己遇到的算法题,希望对大家有所帮助.不定期更新.如果大家想在线运行代码调试,可以将代码拷贝到这里.然后进行调试.下面就是常见的算法题目. 二.正文 1.就n的阶乘.(这 ...
- PTA中如何出Java题目?
PTA中如何出Java题目? 很多第一次出题的老师,不知道Java在PTA中是如何处理输入的.写一篇文章供大家参考.比如以下这样的一个题目: 从控制台读入两个数,然后将其相加输出. 对于该题可以有如下 ...
- Java面试题整理:这些Java程序员面试中经常遇见的题目,必须掌握才能有好结果
1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用时 ...
- java中有关线程的题目
1,看一下下面程序错误发生在哪一行! class Test implements Runnable{ public void run(Thread t){ } } 2,输出结果是什么? class T ...
- JS学习中遇到的一些题目
1.找出所有的水仙花数: 水仙花数例如:153 的特点: 1^3+5^3+3^=153 而且水仙花数只会是三位数,所以可以利用循环的方式来解决问题,循环条件可以设为: var i = 1;i < ...
- JavaScript中对象数组 作业题目以及作业
var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, gender, age, salary) { t ...
随机推荐
- 【原创】Linux基础之用户和组
1 添加.删除用户 # useradd $user# userdel $user 2 设置用户密码 # passwd $user /etc/passwd 3 查看$user的用户和组信息 # id $ ...
- vue路由懒加载
大项目中,为了提高初始化页面的效率,路由一般使用懒加载模式,一共三种实现方式.(1)第一种写法: component: (resolve) => require(['@/components/O ...
- ILMerge参考文档
ILMerge Michael BarnettResearch in Software Engineering (RiSE)Microsoft ResearchCopyright © Microsof ...
- Zombie Scanning
1.theree -way handshake A TCP SYN packet is sent from the device that wishes to establish a connecti ...
- 重写 console.log()
/*重写console.log*/ console.log = (function(mFun){ return function(str){ mFun.call(console,'hello! ' + ...
- Java中CountDownLatch和CyclicBarrier的使用和比较
CountDownLatch和CyclicBarrier同为Java1.5开始引入的,应用于多线程编程中的一种工具,二者用途十分相近,十分容易混淆. CountDownLatch CountDownL ...
- day13.装饰器进阶,迭代器
1.from functools import wraps 这个函数可以保留原来函数的属性 # from functools import wraps def car_time(fun): # @wr ...
- day11.装饰器初识
1.开放封闭原则 原则: 开放封闭原则,对扩展是开放的,对修改是封闭的. 封版概念:当写好一个功能以后,就不可以再修改此函数,避免下面一系列的调用产生错误. 因此产生了装饰器 2.装饰器形成过程 我们 ...
- Android键盘显示和隐藏
一.不自动弹出键盘: 带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法: 方法一:在mainfest文件中把对应的activity设置 a ...
- 禁用JavaScript之后,你的网站表现如何?
一 最近要做一个新官网,需求评审完之后,考虑到官网都是纯静态页面,功能简单,操起vue-cli3几秒内创建好了项目脚手架,开发前,我打开了首页模板文件,看到下面这行字,有了一些思考-- <nos ...