【解题报告】uva10192_Vacation(假期, dp, LCS)
2014-06-02 21:17
519 查看
10192 - Vacation
Time limit: 3.000 secondsProblem E: 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 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.
© 2001 Universidade do Brasil (UFRJ). Internal Contest Warmup 2001.
题目大意:
输入多组数据,每组数据包含两行字符串(包含大小写字母、空格、数字),求其最长公共子序列。输入"#"代表结束。
解题思路:
最长公共子序列LCS(Longest Common Subsequence)问题。
设输入的两个序列为A和B。
定义状态d(i,j),代表A由前i个元素构成的子序列和B中前j个元素构成的子序列的最长公共子序列的长度。
状态转移方程:d(i,j) = max{ d(i-1,j), d(i,j-1), d(i-1, j-1)+1 | A[i]=B[j] }
d(i,j) = max{ d(i-1,j), d(i,j-1) | A[i]!=B[j] }
注意输入的字符串中可能包含空格,用gets来读入。
#include <cstdio> #include <cstring> #define max(a,b) (a>b?a:b) char A[110]; char B[110]; int d[110][110]; int T=1; int main() { while(gets(A+1),strcmp(A+1,"#")){ gets(B+1); memset(d,0,sizeof(d)); int lenA=strlen(A+1); int lenB=strlen(B+1); for(int i=1;i<=lenA;++i){ for(int j=1;j<=lenB;++j){ d[i][j]=max(d[i-1][j], d[i][j-1]); if(A[i]==B[j]) d[i][j]=max(d[i-1][j-1]+1, d[i][j]); } } printf("Case #%d: you can visit at most %d cities.\n",T++,d[lenA][lenB]); } return 0; }
相关文章推荐
- uva10192 - Vacation(dp,lcs)
- 【解题报告】uva111_History Grading(历史考试, dp, LCS)
- UVa 10192 Vacation (DP&LCS)
- 【解题报告】uva10066_The Twin Towers(双塔, dp, LCS)
- UVa 10192 Vacation (DP 最长公共子序列)
- 【解题报告】uva10405_Longest Common Subsequence(最长公共子序列, dp)
- UVa 10192 - Vacation(LCS水题)
- UVa 10192 - Vacation(LCS水题)
- 【解题报告】uva10003_Cutting Sticks(切割木棍, dp)
- 解题报告(二):uva 10029 - Edit Step Ladders(dp+hash)
- uva 10192 Vacation(动态规划:LCS)
- 一中OJ #1434 序列的LCS [UVa 10635 -> Prince and Princess] | 动态规划 LCS映射转换LIS | 解题报告
- 解题报告(一):uva 10313 - Pay the Price (dp)
- 【解题报告】uva103_Stacking Boxes(堆砌盒子, dp)
- 【解题报告】uva10131_Is Bigger Smarter?(越大越聪明, dp, LIS)
- UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
- uva10192 - Vacation (dp 最长公共子序列)
- 判断条件UVa 10192 - Vacation(LCS水题)
- 【解题报告】uva116_Unidirectional TSP(单向TSP, dp)
- 【解题报告】uva147_Dollars(美元, dp, 完全背包)