There are two sequences of length p+1 and q+1, each element in each sequence is different, and all are integers between 1~n*n, the first element of the two series is 1, and the length of the longest common subsequence of A and B is obtained.
The idea is very easy to think of LCS, but because O (PQ) algorithm will definitely time out, so you can not use, notice that the elements in A and B are different, it is possible to preprocess the elements in a, the trans array records a each element value corresponding position, and then process the elements in B, Convert each element to the position of the element in a, if it does not appear so zero in a, so that the LCS problem into the LIS
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const INT MAXN = $ * + 50;const int INF = 0x3f3f3f3f;int N, p, Q;int B[MAXN], TRANS[MAXN], G[MAXN], D[maxn];int Kas E = 0;void init () {scanf ("%d%d%d", &n, &p, &q), memset (trans, 0, sizeof (trans)); for (int i = 1; I <= p+1; i++ {int tmp; SCANF ("%d", &tmp); trans[tmp] = i;} for (int i = 0; I <= q; i++) {int tmp; SCANF ("%d", &tmp); B[i] = trans[tmp];}} void Solve () {int ans =-inf;for (int i = 1; I <= q+1; i++) G[i] = inf;for (int i = 0; I <= q; i++) {int k = Lower_bou nd (g+1, g+q+2, B[i])-g;d[i] = k;g[k] = B[i];} for (int i = 0; I <= q; i++) ans = max (ans, d[i]);p rintf ("Case%d:%d\n", ++kase, ans);} InchT Main () {freopen ("Input.txt", "R", stdin); int t; scanf ("%d", &t), while (t--) {init (); Solve ();} return 0;}
Prince uva10635 and Princess (convert LCS to LIS)