//最长公共子序列
#include <iostream>
#include <cstring>
using namespace std;
int dp(int , int );
int n1;
int n2;
int a1[110];
int a2[110];
int d[110][110];
int main()
{
cin >> n1 >> n2;
int count = 0;
while(n1 || n2)
{
count++;
for(int i = 1; i <= n1; i++)
cin >> a1[i];
for(int i = 1; i <= n2; i++)
cin >> a2[i];
memset(d, 0, sizeof(d));
cout << "Twin Towers #" << count << endl;
cout << "Number of Tiles : " << dp(n1, n2) << endl;
cout << endl;
cin >> n1 >> n2;
}
}
int dp(int n1, int n2)
{
for(int i = 1; i <= n1; i++)
for(int j = 1; j <= n2; j++)
{
if(a1[i] == a2[j])
d[i][j] = d[i-1][j-1] + 1;
else
d[i][j] = d[i][j-1] > d[i-1][j] ? d[i][j-1] : d[i-1][j];
}
return d[n1][n2];
}
UVaOJ_10066 - The Twin Towers
最新推荐文章于 2018-12-05 01:07:04 发布