Search code examples
c++calgorithmsuffix-tree

How to print the string in a suffix tree


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?


Solution

  • 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);