UVA_10192
求最长公共子序列即可。
#include<stdio.h>
#include<string.h>
#define MAXD 110
char b1[MAXD], b2[MAXD], f[MAXD][MAXD];
int init()
{
gets(b1 + 1);
if(b1[1] == '#')
return 0;
gets(b2 + 1);
}
void solve()
{
int i, j, k1, k2;
memset(f, 0, sizeof(f));
k1 = strlen(b1 + 1);
k2 = strlen(b2 + 1);
for(i = 1; i <= k1; i ++)
for(j = 1; j <= k2; j ++)
{
if(b1[i] == b2[j])
f[i][j] = f[i - 1][j - 1] + 1;
else
f[i][j] = f[i - 1][j] > f[i][j - 1] ? f[i - 1][j] : f[i][j - 1];
}
printf("you can visit at most %d cities.\n", f[k1][k2]);
}
int main()
{
int t = 0;
while(init())
{
printf("Case #%d: ", ++ t);
solve();
}
return 0;
}