-1

I am having difficulty in printing the longest common substring in a suffix tree.I can easily calculate the length of the longest common substring but having problems in actually finding the substring.Below is the code for Longest Common Substring in C++.Can anyone please help me out?

user3318603
  • 221
  • 3
  • 12
  • Where are the strings stored? In list? arrays? – Raydel Miranda Mar 12 '14 at 13:02
  • please have a look at the link given below in the question for better understanding – user3318603 Mar 12 '14 at 13:03
  • 1
    The question should be clear without external references. If you don't tell us how the data you want to print is stored, we can't tell you how to print it. – ugoren Mar 12 '14 at 13:10
  • Please have a look now. – user3318603 Mar 12 '14 at 13:13
  • You don't even say which variable contains the string you want to print. – ugoren Mar 12 '14 at 13:28
  • I want to print the longest common substring of strings a and b.The above code only prints the length and not the subtring.I need some modification to print the substring in the above code.Strings x should contain the longest common substring – user3318603 Mar 12 '14 at 13:32
  • @user3318603: Do you know where your strings are stored and how? This is an elementary concept of suffix trees and you should be able to understand it. If not, perhaps you should ask a question about that. – Michael Foukarakis Mar 12 '14 at 13:51

1 Answers1

0

Add variable:

int start = -1;

Replace:

ans=max(ans,l);

with:

if (l > ans) {
  ans = l;
  start = i;
}

The longest substrings starts at b[start], so to print the longest substring in the end:

printf("%.*s", ans, b + start);
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Doesn't the longest string _end_ with `b[i]`, such that `start` should be `start = i + 1 - l`? – M Oehm Mar 12 '14 at 13:55