# ZeroJudge - a676: 00111 - History Grading ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=a676 ###### 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 events, order, history[21], answers[21], DP[21][21] = {}; cin >> events; for (int i = 1; i <= events; ++i) { cin >> order; history[order] = i; } while (cin >> order) { answers[order] = 1; for (int i = 2; i <= events; ++i) { cin >> order; answers[order] = i; } for (int i = 1; i <= events; ++i) for (int j = 1; j <= events; ++j) if (history[i] == answers[j]) DP[i][j] = DP[i - 1][j - 1] + 1; else DP[i][j] = max(DP[i][j - 1], DP[i - 1][j]); cout << DP[events][events] << '\n'; } } ```