UVA 10192 - Vacation
求最长公共子序列,注意到输入有空格,用gets()就可以了。
代码如下:
#include<stdio.h>
#include<string.h>
#define MAXN 100 +10
char m[MAXN], f[MAXN];
int r[MAXN][MAXN], n;
void dp()
{
int lenm = strlen(m+1);
int lenf = strlen(f+1);
for(int i = 1; i <= lenm; i ++)
for(int j = 1; j <= lenf; j ++)
{
if(m[i] == f[j]) r[i][j] = r[i-1][j-1] + 1;
else if(r[i][j-1] > r[i-1][j]) r[i][j] = r[i][j-1];
else r[i][j] = r[i-1][j];
}
printf("Case #%d: you can visit at most %d cities.\n",n,r[lenm][lenf]);
}
void input()
{
n = 0;
while(gets(m+1) != NULL)
{
if(m[1] == '#') break;
gets(f+1);
n ++;
memset(r,0,sizeof(r));
dp();
}
}
int main()
{
input();
return 0;
}