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() {
	cas = 1;
	while(gets(s1 + 1), s1[1] != '#') {
		gets(s2 + 1);
		int len1 = strlen(s1 + 1), len2 = strlen(s2 + 1);
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= len1; i++) {
			for(int j = 1; j <= len2; j++) {
				if(s1[i] == s2[j]) 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 most %d cities.\n", cas++, dp[len1][len2]);
	}
	return 0;
}
时间: 2024-10-11 06:01:47

UVA - 10192 - Vacation (LCS)的相关文章

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 (最长公共子序列)

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 - 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 - 10534Wavio Sequence(LIS)

题目:UVA - 10534Wavio Sequence(LIS) 题目大意:给出N个数字,找出这样的序列:2 * n + 1个数字组成.前面的n + 1个数字单调递增,后面n + 1单调递减. 解题思路:从前往后找一遍LIS,再从后往前找一遍LIS.最后只要i这个位置的LIS的长度和LDS的长度取最小值.再*2 - 1就是这个波浪数字的长度.注意这里的求LIS要用nlog(n)的算法,而且这里的波浪数字的对称并不是要求i的LIS == LDS,而是只要求LIS和LDS最短的长度就行了,长的那个

最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij=yj.例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列. 考虑最长公共子序列问题如何分解成

【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)

昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以就地利用mysql,最易行.方案四:使用方案三,但增加一个表以减少冗余,但代价新开一个表,并且每次查询都select join两个表. 研究了 求最长公共子串问题,顺便研究了字符串匹配 字符串匹配的Boyer-Moore算法http://www.ruanyifeng.com/blog/2013/05/boy

uva 725 Division(除法)暴力法!

uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that t

Uva 11889 - Benefit( 数论 )

Uva 11889 - Benefit( 数论 ) 题意: calculate the lowest integer B such that LCM(A, B) = C 分析: LCM(A,B) = C = A*B/GCD(A,B)C*GCD(A,B) = A*BC/A = B/GCD(A,B)如果C%A != 0 无解否则, 令t = C/AB = t * GCD(A,B) 即B 一定是 t 的整数倍从t开始枚举B #include <cstdio> typedef long long LL

UVA 10139 Factovisors(数论)

Factovisors The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n > 0) We say that a divides b if there exists an integer k such that k*a = b The input to your program consists of several lines, each conta