First, we need to deal with the input. The general idea of solving the problem is to rearrange the given c array and r array according to the rank of each historical event, that is, the number of the earliest event is placed in the first place of the array ...... then this question is transformed into the question of finding the longest common sub-sequence length of two strings.
However, I used another solution (although dynamic planning is still used =-= ):
Only sorts the input c array (that is, the serial number of the event where the rank is I in the c array), and the r array remains unchanged. Create an ans array. ans [I] stores the longest sequence length ending with rank I, and the initialization value is 1.
The program fills the ans array starting from 0th. When executed to evaluate ans [I], respectively judge rank events from 0-i-1, such as j events, in the student's answer (that is, the data in the r array) if the time before the event is also before the I event, use ans [j] + 1 to update ans [I] (because ans [I] is initialized to 1 ). After the ans array is filled, the maximum value is the result.
My code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <string> # include <algorithm> using namespace std; int c [20], r [20]; int ans [20]; int N; int main () {cin> N; int ci; for (int I = 0; I <N; I ++) {// rearrange the c array cin in chronological order> ci; c [ci-1] = I;} int tmp; while (cin> tmp) {r [0] = tmp; ans [tmp] = 0; for (int I = 1; I <N; I ++) cin> r [I]; int maxlen = 1; ans [0] = 1; for (int I = 1; I <N; I ++) {ans [I] = 1; // judge Disconnection event 0 ~ I-1for (int j = 0; j <I; j ++) {if (r [c [j] <r [c [I]) {if (ans [I] <(ans [j] + 1) ans [I] = ans [j] + 1 ;}} if (maxlen <ans [I]) maxlen = ans [I];} cout <maxlen <endl;} return 0 ;}