本文主要是介绍uva 10066 LCS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
最长公共子序列。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>#define LL long longusing namespace std;
const int maxn = 100 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);int a1[maxn], a2[maxn];
int dp[maxn][maxn];int main()
{#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALint n1, n2;int ncase = 1;while (scanf("%d%d", &n1, &n2) == 2){if (n1 == 0 && n2 == 0)break;for (int i = 0; i < n1; i++){scanf("%d", &a1[i]);}for (int i = 0; i < n2; i++){scanf("%d", &a2[i]);}memset(dp, 0, sizeof(dp));for (int i = 1; i <= n1; i++){for (int j = 1; j <= n2; j++){if (a1[i - 1] == a2[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", ncase++);printf("Number of Tiles : %d\n\n", dp[n1][n2]);}return 0;
}
这篇关于uva 10066 LCS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!