Confused101's blog

By Confused101, history, 9 years ago, In English

I am solving LCS problem from spoj, which is

input: 2 strings of length atmax 250000 characters (S1 & S2) Output: length of the longest common substring of them.

my approach:

   n1 = S1.length() , n2 = S2.length();
   S = S1 + '#' + S2;

   build Suffix_Array (SA[]) and LCP_Array (LCP[]) of string S;
   Ans = 0;
   for(i = 1 to n1+n2 ):
      if( (SA[i-1] < n1 && SA[i] > n1) or (SA[i-1] > n1 && SA[i] < n1))
         if(LCP[i] > Ans)
            Ans = LCP[i];
   return Ans;

Which is giving TLE as time limit for the problem is too strict (0.294s). Can someone please help me with this. Thanks !

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

you should use O(n) algorithm to build suffix array. Try this: https://sites.google.com/site/indy256/algo_cpp/suffix_array_lcp

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I hope that will work, but any ideas how to get AC using O(nlogn) Suffix_array & Lcp_array implementation ?

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I believe it is not possible. Time limit is too strict and N is too big.

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I was looking for some small optimization, Anyways it is better to learn O(n) Suffix_array implementation. Thanks for your views :).