又一道lcs,不多说,本blog有大量lcs题目解释
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 100+10
int first[MAXN], second[MAXN], dp[MAXN][MAXN];
int dynamic_programming(const int &len_first, const int &len_second)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= len_first; i ++) {
for(int j = 1; j <= len_second; j ++) {
if( first[i] == second[j] ) {
dp[i][j] = dp[i-1][j-1]+1; continue;
}
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[len_first][len_second];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int len_first, len_second, cas(1);
while( scanf("%d %d", &len_first, &len_second) ) {
if( !len_first && !len_second ) {
break;
}
for(int i = 1; i <= len_first; i ++) {
scanf("%d", &first[i]);
}
for(int i = 1; i <= len_second; i ++) {
scanf("%d", &second[i]);
}
printf("Twin Towers #%d\n", cas ++);
printf("Number of Tiles : %d\n\n", dynamic_programming(len_first, len_second));
}
return 0;
}
uva_10066 The Twin Towers
最新推荐文章于 2017-08-09 16:47:03 发布