uvaoj 10192 - Vacation 最长公共子序列(LCS)

来源:互联网 发布:结构体包含数组memset 编辑:程序博客网 时间:2024/06/06 05:07
10192 - Vacation
父亲和母亲都给定一个旅行的建议,现在,要尽量满足两个人的要求,还要旅行更多的城市,就是最长公共子序列.注意可能有空串.
代码如下:
/*************************************************************************> File Name: 10192.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月12日 星期三 21时23分27秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;#define N 110int dp[N][N];string s1, s2;/* * 最长公共子序列,注意字符串可能为空串 */int main(int argc, char *argv[]){int c = 0;while (getline(cin, s1)) {if (s1 == "#") {break;} else {getline(cin, s2);}int l1= s1.length();int l2= s2.length();clr(dp, 0);for (int i = 1; i <= l1; ++i) {for (int j = 1; j <= l2; ++j) {if (s1[i - 1] == s2[j - 1]) {dp[i][j] = dp[i - 1][j - 1] + 1;} else {dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);}}}printf("Case #%d: you can visit at most %d cities.\n", ++c, dp[l1][l2]);}return 0;}/*The ProblemYou are planning to take some rest and to go out on vacation, but you reallydon'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'replanning to travel, go first to Paris, then to Lisboa, then to London andthen, 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'reafraid 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'ssuggestion. But it can get worse, because you can hurt both of them if yousimply ignore their suggestions!Thus, you decide that you'll try to follow their suggestions in the betterway that you can. So, you realize that the "Paris-Lisboa-London" order isthe one which better satisfies both your mother and your father. Afterwardsyou can say that you could not visit Madrid, even though you would've likedit very much.If your father have suggested the "London-Paris-Lisboa-Madrid" order, thenyou would have two orders, "Paris-Lisboa" and "Paris-Madrid", that wouldbetter satisfy both of your parent's suggestions. In this case, you couldonly visit 2 cities.You want to avoid problems like this one in the future. And what if theirtravel suggestions were bigger? Probably you would not find the better wayvery 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 63different cities to visit. But it's possible that you'll visit some citymore than once.If you represent Paris with "a", Madrid with "b", Lisboa with "c" and Londonwith "d", then your mother's suggestion would be "abcd" and you father'ssuggestion would be "acdb" (or "dacb", in the second example).The program will read two travel sequences and it must answer how manycities you can travel to such that you'll satisfy both of your parents andit's maximum.The InputThe input will consist on an arbitrary number of city sequence pairs. Theend of input occurs when the first sequence starts with an "#"character(without the quotes). Your program should not process this case. Eachtravel sequence will be on a line alone and will be formed by legalcharacters (as defined above). All travel sequences will appear in asingle line and will have at most 100 cities.The OutputFor 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 themaximum number of cities you can visit such that you'll satisfy both youfather's suggestion and you mother's suggestion.Sample Inputabcdacdbabcddacb#Sample OutputCase #1: you can visit at most 3 cities.Case #2: you can visit at most 2 cities.*/


0 0