您的位置:首页 > 其它

UVA 10192 - Vacation

2014-10-21 22:44 274 查看
还是dp最长公共子序列。

#include <iostream>
#include <limits>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

const int size = 100 + 10;

char a[size] , b[size];
int d[size][size];

int dp(int x , int y)
{
if(d[x][y] >=0) return d[x][y];
if(!x || !y) return d[x][y] = 0;

if(a[x-1] == b[y-1]) return d[x][y] = dp(x-1 , y-1) + 1;
else return d[x][y] = max(dp(x-1 , y) , dp(x , y-1));
}
int main()
{
int cnt = 0;
while(fgets(a , size , stdin) && a[0] != '#' && fgets(b , size , stdin))
{
int a_len = strlen(a);
int b_len = strlen(b);
memset(d , -1 , sizeof(d));
printf("Case #%d: you can visit at most %d cities.\n" , ++cnt , dp(a_len-1 , b_len-1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: