UVA 10192 Vacation (longest common sub-sequence)

Source: Internet
Author: User

UVA 10192 vacationthe problem

You're planning to take some rest and to go off on the vacation, but you really don ' t know which cities you should visit. So, your 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 am I ' m talking about. "

Now your ' re a bit confused, as you didn ' t expected this situation. You ' re afraid so you'll hurt your mother if you follow your ' s father. But your ' re also afraid to hurt your father if you follow ' s mother. But it can get worse, because you can hurt both of them if you simply ignore their suggestions!

Thus, you decide so you'll try to follow their suggestions in the better-on-the-you-can. So, you realize the ' Paris-lisboa-london ' order is the one which better satisfies both your mother and your father. Afterwards you can say this could not visit Madrid, even though you would ' ve liked it very much.

If Your father has suggested the "London-paris-lisboa-madrid" order, then you would has both orders, "Paris-lisboa" and " Paris-madrid ", that would better satisfy both of your of the 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 would not find the better to very easy. So, you decided to the write a program to the "this task." You'll represent each city by one character, using uppercase letters, lowercase letters, digits and the space. Thus, you can has at most the 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 is "acdb" (or "DACB", in the second example).

The program would read the sequences and it must answer how many cities you can travel to such so you ' ll satisfy B Oth of your parents and it ' s maximum.

The Input

The input would consist on a 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 is not a process this case. Each travel sequence'll be is on a line alone and would be formed by legal characters (as defined above). All travel sequences would appear in a and would have the most cities.

The Output

For each sequence pair, you must print the following message with a line alone:

Case #d: You can visit at the most K cities.

Where D stands for the test Case number (starting from 1) and K are the maximum number of cities can visit such that yo U ' ll satisfy both you father's suggestion and you mother ' s suggestion.

Sample Input

Abcd

Acdb

Abcd

Dacb

#

Sample Output

Case #1: Your can visit at the most 3 cities.

Case #2: Your can visit at the most 2 cities.

Title: The longest common subsequence. The idea of solving problems: the longest common subsequence. Be careful to read with get.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define Nusing namespace STD;CharA[n], b[n];intDp[n][n];intMain () {intCase =1; while(Gets (a) = NULL) {if(a[0] ==' # ') Break; Gets (b);memset(DP,0,sizeof(DP));intL1 =strlen(a), L2 =strlen(b);intMax =0; for(inti =1; I <= L1; i++) { for(intj =1; J <= L2; J + +) {if(A[i-1] = = B[j-1]) {Dp[i][j] = dp[i-1][j-1] +1; }Else{Dp[i][j] = max (Dp[i-1][J], Dp[i][j-1]); }            }        }printf("Case #%d:you can visit at the most%d cities.\n", case++, Dp[l1][l2]); }return 0;}

UVA 10192 Vacation (longest common sub-sequence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.