uva 10066 The Twin Towers (最长公共子)

uva 10066 The Twin Towers

标题效果:最长公共子。


解题思路:最长公共子。

#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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-10 19:06  hrhguanli  阅读(126)  评论(0编辑  收藏  举报