# ZeroJudge - a133: 10066 - The Twin Towers ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=a133 ###### tags: `ZeroJudge` `動態規劃(Dynamic Programming)` `最長共同子序列(Longest Common Subsequence)` ```cpp= #include <iostream> #include <algorithm> using namespace std; int main() { cin.sync_with_stdio(false); cin.tie(nullptr); int timeCount = 0, tiles1, tiles2, kinds1[101], kinds2[101], LCS[101][101] = {}; while (cin >> tiles1 >> tiles2, tiles1 || tiles2) { ++timeCount; for (int i = 1; i <= tiles1; ++i) cin >> kinds1[i]; for (int i = 1; i <= tiles2; ++i) cin >> kinds2[i]; for (int i = 1; i <= tiles1; ++i) for (int j = 1; j <= tiles2; ++j) if (kinds1[i] == kinds2[j]) LCS[i][j] = LCS[i - 1][j - 1] + 1; else LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]); cout << "Twin Towers #" << timeCount << "\nNumber of Tiles : " << LCS[tiles1][tiles2] << "\n\n"; } } ```