有两座塔,由不同的石头组成,现在要去掉一些石头让两个塔一样,求塔最后剩下的最大高度。
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int n1, n2;
int a[105], b[105];
int f[105][105];
int main() {
int i, j, kase=0;
while(scanf("%d %d", &n1, &n2) != EOF && (n1 + n2)) {
for(i=1; i<=n1; i++)
scanf("%d", &a[i]);
for(j=1; j<=n2; j++)
scanf("%d", &b[j]);
printf("Twin Towers #%d\n", ++kase);
memset(f, 0, sizeof(f));
for(i=1; i<=n1; i++)
for(j=1; j<=n2; j++) {
if(f[i][j] < f[i][j-1]) f[i][j] = f[i][j-1];
if(f[i][j] < f[i-1][j]) f[i][j] = f[i-1][j];
if(a[i] == b[j] && f[i][j] < f[i-1][j-1] + 1) {
f[i][j] = f[i-1][j-1] + 1;
}
}
printf("Number of Tiles : %d\n", f[n1][n2]);
printf("\n");
}
return 0;
}