UVA_10066

    求最长公共子序列即可。

#include<stdio.h>
#include<string.h>
#define MAXD 110
int N1, N2, a1[MAXD], a2[MAXD], f[MAXD][MAXD];
int init()
{
int i;
scanf("%d%d", &N1, &N2);
if(!N1 && !N2)
return 0;
for(i = 1; i <= N1; i ++)
scanf("%d", &a1[i]);
for(i = 1; i <= N2; i ++)
scanf("%d", &a2[i]);
return 1;
}
void solve()
{
int i, j;
memset(f, 0, sizeof(f));
for(i = 1; i <= N1; i ++)
for(j = 1; j <= N2; j ++)
{
if(a1[i] == a2[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("Number of Tiles : %d\n", f[N1][N2]);
}
int main()
{
int t = 0;
while(init())
{
printf("Twin Towers #%d\n", ++ t);
solve();
printf("\n");
}
return 0;
}


posted on 2011-11-03 20:51  Staginner  阅读(275)  评论(0编辑  收藏  举报