UVa 10192 Vacation (最长公共子序列)

Vacation

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Description

 

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.

当初使用cin读入数据,但是后来不会控制那个“#”结束程序,就改为用gets了,好麻烦的说。。。。
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string.h>
 4 #include<cmath>
 5 #include<cstdio>
 6 using namespace std;
 7 const int MAXN =10010;
 8 int dp[MAXN][MAXN]={0};
 9 int main()
10 {
11     int len1,len2,count=0;
12     char str1[MAXN],str2[MAXN];
13     while(gets(str1)&& str1[0]!=‘#‘)
14     {
15         gets(str2);
16         len1=strlen(str1);
17         len2=strlen(str2);
18         for(int i=1;i<=len1;i++)
19         {
20             for(int j=1;j<=len2;j++)
21             {
22                 if(str1[i-1]==str2[j-1])
23                     dp[i][j]=dp[i-1][j-1]+1;
24                 else
25                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
26             }
27         }
28         count++;
29         printf("Case #%d: you can visit at most %d cities.\n",count,dp[len1][len2]);
30     }
31     return 0;
32 }
				
时间: 2024-08-24 13:37:33

UVa 10192 Vacation (最长公共子序列)的相关文章

UVA 10723--Cyborg Genes+最长公共子序列变形

题目链接:点击进入 首先对于长度最短的情况是很容易确定的,只需要用两个字符串的长度和减去他们的最长公共子序列长度.然后比较麻烦的就是合乎要求的字符串的个数,其实我们也可以用类似于最长公共子序列的dp来求. 设dp[i][j]表示str1的前i个字符和str2的前j个字符所得到的满足要求的字符串,则如果str[i]==str[j],则dp[i][j]+=dp[i-1][j-1]; 否则就要根据i,j这两个位置上的最长公共子序列长度进行讨论,具体见代码. 代码如下: #include<iostrea

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 an

UVa 10192 - Vacation ( LCS 最长公共子串)

链接:UVa 10192 题意:给定两个字符串,求最长公共子串的长度 思路:这个事最长公共子串的直接应用 #include<stdio.h> #include<string.h> int max(int a,int b) { return a>b?a:b; } int main() { char s[105],t[105]; int i,j,k=0,m,n,dp[105][105]; while(gets(s)!=NULL){ if(strcmp(s,"#"

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

uva 10066 The Twin Towers (最长公共子序列)

uva 10066 The Twin Towers 题目大意:最长公共子序列. 解题思路:最长公共子序列. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

UVA 10635 Prince and Princess 最长公共子序列(nlongn)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19051 题目意思是给你两串数字(计为 a,b 数组),让你求他们的最长公共子序列.数字长度是 n * n (n <= 250)所以 O(n^2) 肯定过不了了.所以想要找 O(nlongn)的. 因为题目给出了数字在 1 ~ (n^2)内并且数字不会重复.因此,对于a数组中的每个数字,如果我们都知道他在b数组中出先得位置的话(位置计为 c 数组),我们只需要在c数组里面求一

UVA - 10635 最长公共子序列

input n,p,q 2<=n<=250 1<=p,q<=n*n 1 a1 a2 a3 ... ap 1<ai<n*n,ai!=aj 1 b1 b2 b3 ... bq 1<bi<n*n,bi!=bj output 最长公共子序列个数 做法:将b数组中的数变为a数组中数的下标,a中不存在的数可以去掉,然后求LIS即可 #include <cstdio> #include <queue> #include <cstring>