For a set of elements that are different from each other, find the longest common subsequence of two different permutations of them
Record the position number of each element of the first sequence, and assign the second sequence to the position number of the element
The problem then turns into finding the longest ascending subsequence
*********************************************************************************************************
#include <cstdio> using namespace std; const int maxn = 1e5 + 10; const int INF = 1e6; int a[maxn], hash_[maxn], dp[maxn]; int bin_find(int key, int l, int r); intmain() { int i; int n, x; scanf("%d", &n); for(i = 1; i <= n; ++i) { scanf("%d", &x); hash_[x] = i; } for(i = 1; i <= n; ++i) { scanf("%d", &x); a[i] = hash_[x]; } int len = 0; dp[0] = -INF; for(i = 1; i <= n; ++i) { if(a[i] > dp[len]) dp[++len] = a[i]; else { int pos = bin_find(a[i], 1, len); dp[pos] = a[i]; } } printf("%d\n", len); return 0; } int bin_find(int key, int l, int r) { while(l <= r) { int mid = (l+r)>>1; if(key <= dp[mid]) r = mid - 1; else l = mid + 1; } return r + 1; }