题目大意:最长公共子序列。
解题思路:最长公共子序列。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[105], b[105], dp[105][105];
int main() {
int n, m, Case = 1;
while (scanf("%d %d", &n, &m) == 2) {
if (n == 0 && m == 0) break;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d", &b[i]);
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1]) {
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", Case++);
printf("Number of Tiles : %d\n\n", dp[n][m]);
}
return 0;
}
uva 10066 The Twin Towers (最长公共子序列)
原文地址:http://blog.csdn.net/llx523113241/article/details/44261903