UVa 10192: Vacation

依旧LCS,dp解决,套模板即可。

注意字符串中可以有空格。

解题代码如下:

 

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <algorithm>



using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))

#define maxn 105

char Seq1[maxn], Seq2[maxn];

int N1, N2;

int LongLen[maxn][maxn];

int dp(int i, int j)

{

	if(i==-1 || j==-1) return 0;

	if(LongLen[i][j]>=0) return LongLen[i][j];

	if(Seq1[i]==Seq2[j]) return LongLen[i][j] = dp(i-1,j-1)+1;

	else return LongLen[i][j] = max( dp(i-1,j), dp(i,j-1));

}

int main()

{

	int Case = 0;

	while(gets(Seq1) && Seq1[0]!='#')

	{

		gets(Seq2);

		N1 = strlen(Seq1); N2 = strlen(Seq2);

		memset(LongLen,-1,sizeof(LongLen));

		printf("Case #%d: you can visit at most %d cities.\n", ++Case, dp(N1-1,N2-1));

	}

	return 0;

}


 

 

你可能感兴趣的:(uva)