Question
Refer to ,,,
// Find the longest common subsequence of all sequences, // define the State DP [I] as the length of the longest common subsequence of the first I digit in the first sequence, // The state transition equation is DP [I] = max (DP [I], DP [J] + 1 ); j <I // pre-process the positional relationship between two numbers in all sequences. // For example, if two numbers a and B exist in any sequence, as long as a is behind B, after [a] [B] = 1. // If! After [a] [B], the status is transferred. # Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int A [6] [1010]; int after [1010] [1010]; int DP [1010]; int main () {int N, K; scanf ("% d", & N, & K); int II = 0; for (INT I = 0; I <K; I ++) {for (Int J = 0; j <n; j ++) {scanf ("% d ", & A [I] [J]) ;}} memset (after, 0, sizeof (after); memset (DP, 0, sizeof (DP )); for (INT I = 0; I <K; I ++) {for (Int J = 0; j <n; j ++) {for (int K = J + 1; k <n; k ++) {after [A [I] [k] [A [I] [J]] = 1; // K exists after J} int ans = 0; // directly to 1 ~ N status transfer is not allowed // DP for (INT I = 0; I <n; I ++) {DP [I] = 1 according to the first line; for (Int J = 0; j <I; j ++) {If (after [A [0] [J] [A [0] [I] = 0) DP [I] = max (DP [I], DP [J] + 1);} ans = max (DP [I], ANS);} printf ("% d \ n", ANS); Return 0 ;}
View code
Codeforces 463d gargari and permutations (DP)