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 ...
随机推荐
- 【原创】大叔经验分享(18)hive2.0以后通过beeline执行sql没有进度信息
一 问题 在hive1.2中使用hive或者beeline执行sql都有进度信息,但是升级到hive2.0以后,只有hive执行sql还有进度信息,beeline执行sql完全silence,在等待结 ...
- linux redis 主从复制
在从服务的redis.conf 添加 slaveof 主服务器 端口 查看reids进程和端口,都是存在的.只是ip地址是127.0.0.1而不是0.0.0.0,只是本机能使用; 查找redis的配置 ...
- extensible_index
create user ex identified by oracle; grant Resource to ex;grant connect to ex;grant create view to e ...
- springMVC下载中文文件名乱码【转】
//遇到的现象是,下载含有中文文件名的文件时,能获取到文件,但是使用IE正常,使用firefox,chrome文件名却乱码.//既然如此,就区分一下浏览器再返回好了,处理方式如下 //RESTfull ...
- SQL反模式学习笔记7 多态关联
目标:引用多个父表 反模式:使用多用途外键.这种设计也叫做多态关联,或者杂乱关联. 多态关联和EAV有着相似的特征:元数据对象的名字是存储在字符串中的. 在多态关联中,父表的名字是存储在Issue_T ...
- 这里主要展示在Win7下怎么用IIS发布局域网站
首先对IIS做一个简要的介绍: IIS(InternetInformationServices)互联网信息服务的简称.本质是一种Web(网页)服务组件,其中包含Web.FTP和SMTP三大服务器,分别 ...
- Manjaro为包管理器pacman和yaourt\yay 添加多线程下载
用轻量级的axle代替了默认的wget来下载升级包. 1. paman添加多线程 编辑pacman.conf文件: vim /etc/pacman.conf 如果有类似xfercommand的话,注释 ...
- iOS12系统应用发送邮件中的附件
iOS12系统应用发送邮件中的附件 iOS12系统应用发送邮件中的附件,如果发送邮件的内容很多,或者包含文档.图片等,可以以附件的形式进行发送.此时需要使用addAttachmentData(_:mi ...
- 移动端click事件出现300ms延迟
问题分析: 双击缩放是指手在屏幕上快速点击两次,iOS自带的Safari浏览器会将网页缩放至原始比例.当用户在屏幕上单击某元素时,浏览器会先捕获此处单击,但浏览器不知道用户是要单击链接还是要双击该部分 ...
- 第四篇flask中模板语言 jinja2
Flask中默认的模板语言是Jinja2 首先我们要在后端定义几个字符串,用于传递到前端 STUDENT = {, 'gender': '中'}, STUDENT_LIST = [ {, 'gende ...