Confused101's blog

By Confused101, history, 11 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

»
11 years ago, hide # |
 
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