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 !