题目大意:输出最长公共子序列长度
解题策略:DP
/*
UVA 10066 The Twin Towers
AC by J.Dark
ON 2013/3/6
Time 0.028s
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1010
using namespace std;
int s1[MAXN], s2[MAXN];
int dp[MAXN][MAXN];
int max( int a, int b)
{
return a > b ? a : b;
}
void input(int num, int p[]){
for(int i=1; i<=num; i++)
cin >> p[i];
}
int main()
{
int s1num, s2num, count1=0;
while( cin >> s1num >> s2num && (s1num || s2num))
{
input(s1num, s1);
input(s2num, s2);
memset(dp, 0, sizeof(dp) );
for( int i = 1; i <= s1num; i ++){
for( int j = 1; j <= s2num; j ++)
{
if( s1[i] == s2[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max( dp[i - 1][j], dp[i][j - 1]);
}
}
printf("Twin Towers #%d\n", ++count1);
printf( "Number of Tiles : %d\n\n", dp[s1num][s2num]);
}
return 0;
}