I'm trying to print the longest common subsequence of 2 long long vectors in O(nlogn) worst case time, and I have the following three conditions- 1. Each element in a vector is distinct 2. Each vector is a permutation of the other 3. If the size of the vector is S, then the elements are all the integers from 1 to S I know there is a way to find the length of the LCS in O(nlogn) complexity, which I achieved in this way-
long long lcs_length(vector<long long> A, vector<long long> B)
{
vector<long long> C(A.size());
vector<long long> look_up(A.size());
for(long long i = 0; i < A.size(); i++)
{
look_up[A[i]-1] = i;
}
for(long long i = 0; i < A.size(); i++)
{
C[i] = look_up[B[i]-1];
}
vector<int> d(A.size()+1, 1000000000);
for (int i = 0; i < A.size(); i++)
{
*lower_bound(d.begin(), d.end(), C[i]) = C[i];
}
for (int i = 0; i <= A.size(); i++)
{
long long temp;
if (d[i] == 1000000000)
{
return i;
}
}
return 0;
}
Is there a way to actually print the LCS in O(nlogn) time?