标签:
题目链接 http://acm.hust.edu.cn/vjudge/problem/19204
解题思路
LCS问题。
代码
#include<stdio.h> #include<string.h> #include<iostream> #define MAX_SIZE 105 //#define LOCAL using namespace std; int dp[MAX_SIZE]; int town1[MAX_SIZE], town2[MAX_SIZE]; int main() { #ifdef LOCAL freopen("data.txt", "r", stdin); freopen("ans.txt", "w", stdout); #endif int T1, T2; int cases = 0; int x; scanf("%d%d", &T1, &T2); while(!(T1 == 0 && T2 == 0)) { memset(dp, 0, sizeof(dp)); for(int i=1; i<=T1; i++) scanf("%d", &town1[i]); for(int i=1; i<=T2; i++) scanf("%d", &town2[i]); for(int i=1; i<=T1; i++) { x = dp[0]; for(int j=1; j<=T2; j++) { if(town1[i] == town2[j]) { int y = dp[j]; dp[j] = x+1; x = y; } else { x = dp[j]; dp[j] = max(dp[j], dp[j-1]); } } } cases++; printf("Twin Towers #%d\n", cases); printf("Number of Tiles : %d\n", dp[T2]); printf("\n"); scanf("%d%d", &T1, &T2); } return 0; }
标签:
原文地址:http://www.cnblogs.com/ZengWangli/p/5785106.html