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 ...
随机推荐
- Confluence 使用常见问题列表
Confluence 6 管理 Atlassian 提供的 App 摘要: Confluence 用户可以使用桌面应用来编辑一个已经上传到 Confluence 的文件,然后这个文件自动保存回 Con ...
- C语言对字符串去重
# include <stdio.h> # include <string.h> char * getNewChar(char * str,char * newStr); in ...
- Spring Boot项目部署到外部Tomcat服务器
2017年04月27日 23:33:52 阅读数:7542 前言 Spring Boot项目一般都是内嵌tomcat或者jetty服务器运行,很少用war包部署到外部的服务容器,即使放到linux中, ...
- ios中safari无痕浏览模式下,localStorage的支持情况
前言 前阶段,测试提了个bug,在苹果手机中无痕模式下,搜索按钮不好使,无法跳页,同时搜索历史也没有展示(用户搜索历史时使用localStorage存储). 正文 iOS上Sarfari在无痕模式下, ...
- 饮冰三年-人工智能-Python-19 Python网络编程
Socket:套接字.作用:我们只需要安照socket的规定去编程,就不需要深入理解tcp/udp协议也可以实现 1:TCP协议 1.1 客户端服务端循环收发消息 # 1:引入stock模块(导包) ...
- RabbitMQ 消息队列 二
一:查看MQ的用户角色 rabbitmqctl list_users 二:添加新的角色,并授予权限 rabbitmqctl add_user xiaoyao 123456 rabbitmqctl se ...
- matplotlib柱状图-【老鱼学matplotlib】
柱状图在平常的图表中是非常常用的图,本节我们来看下如何来显示柱状图. 代码为: import numpy as np import pandas as pd import matplotlib.pyp ...
- Linux远程登录ssh免密码配置方法(仅供参考)
这篇文章主要介绍了linux远程登录ssh免密码配置方法,需要的朋友可以参考下(http://www.0834-3659999.com) 一.情景 公司刚上几台Linux,现在要把主机之间都能远程ss ...
- win10下如何解决U盘连接上电脑但不显示的问题
问题:U盘插上电脑之后,任务栏上有U盘连接上的显示,但是在磁盘符和U盘管理器上没有它的显示. 方法: 1.在任务栏上点击win图标,再点击“设置”(或直接使用快捷键win+i)进入到win10下的“设 ...
- Servlet(九):web.xml文件和server.xml文件
Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...