Give the correct time table for 1-N events and the time table for 1-N events to calculate the maximum number of events, which is the same as the correct order. Solution: Sort Events in order of ranking, and then find the length of the longest common subsequence. Dp [I] [j] = dp [I-1] [J-1] + 1 (save1 [I] = save [j]) dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]); Code: # include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <queue> # define eps 1e-6 # define INF (1 <20) # define PI acos (-1.0) using namespace std; int save1 [30], save2 [30]; int dp [30] [30]; int main () {int n, temp; Scanf ("% d", & n); for (int I = 1; I <= n; I ++) {scanf ("% d", & temp ); save1 [temp] = I; // sort events in the ranking order so that the longest common subsequences have the same rank.} while (scanf ("% d ", & temp )! = EOF) {save2 [temp] = 1; for (int I = 2; I <= n; I ++) {scanf ("% d", & temp ); save2 [temp] = I;} www.2cto.com memset (dp, 0, sizeof (dp); for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) {if (save1 [I] = save2 [j]) dp [I] [j] = dp [I-1] [J-1] + 1; // max (dp [I-1] [J-1] + 1, dp [I-1] [j]), dp [I] [J-1]); else dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]);} printf ("% d \ n", dp [n] [n]);} return 0 ;}