UVA 10192 Vacation

DP,也是一样,求LCS。

不过用scanf会WA。 gets就AC了。 应该是数据中存在空格或者制表符问题。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 100+1
using namespace std;

char a[SIZE],b[SIZE];
int dp[SIZE][SIZE];

int main()
{
    int ca=1;
    while(gets(a)!=NULL)
    {
        if(a[0]=='#')return 0;
        gets(b);

        CLR(dp,0);
        int la=strlen(a);
        int lb=strlen(b);

        FOR(i,0,la)
        FOR(j,0,lb)
        {
            if(a[i]==b[j])
                dp[i+1][j+1]=dp[i][j]+1;
            else
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
        }
        pf("Case #%d: you can visit at most %d cities.\n",ca++,dp[la][lb]);
    }
}
时间: 2024-10-25 05:07:26

UVA 10192 Vacation的相关文章

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 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 思路:就是简单的最长公共子序列啦,不过输入居然还包含空格,然后很奇怪的TLE了,不是WA,心想n最大才100居然TLE,,好吧,可能有些数据特殊吧 AC代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[105], s2[105]; int dp[105][105]; int cas; int main()

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 paren

Uva 10192&amp;10066-(DP)

题目链接:点击打开链接  点击打开链接 两道都是LCS..裸 水过 10192: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #include <cm

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

LCS最长公共子串(复习复习)- -

详细解析LCS:传送通道 重点: 动态规划算法分以下4个步骤: 描述最优解的结构 递归定义最优解的值 按自底向上的方式计算最优解的值   //此3步构成动态规划解的基础. 由计算出的结果构造一个最优解.   //此步如果只要求计算最优解的值时,可省略 解决LCS问题,你要求三个方面的东西:1.LCS(Xm-1,Yn-1)+1:2.LCS(Xm-1,Y),LCS(X,Yn-1):3.max{LCS(Xm-1,Y),LCS(X,Yn-1)}. show me the code: 1 Procedur

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte