0

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?

  • 1
    Just to make sure - have you checked at least Wiki https://en.wikipedia.org/wiki/Longest_common_subsequence_problem ? It says, that algorithm for O((n+r)log(n)) exists and there is link to the paper - have you checked this? – The Godfather Nov 24 '17 at 15:06
  • Yes, I have read that, but my question is can I specifically carry on with the method in the code I've provided, with the vectors of indices. I don't really understand linked lists well. – Kaustubh Varshney Nov 24 '17 at 15:16

0 Answers0