0

Here is the code: I am able to find the longest common substring but I would like to find the longest common substring with at most one skip ?

#include <iostream> 

using namespace std; 

string X,Y; 

int lcs(int i, int j, int count) 
{ 

    if (i == 0 || j == 0) 
        return count; 
    
    if (X[i-1] == Y[j-1]) { 
        count = lcs(i - 1, j - 1, count + 1); 
    } 
    count = max(count, max(lcs( i, j - 1, 0), lcs( i - 1, j, 0))); 
    return count; 
} 



int main() 
{      
    int n,m; 

    X = "AACBkkkk"; 
    Y = "AABDkkkks"; 
    n=X.size(); 
    m=Y.size(); 
    cout<<lcs(n,m,0); 

    return 0; 
} 
Evg
  • 25,259
  • 5
  • 41
  • 83
  • I used inner recursion to find if the next char is equal or not – Desmond9989 Jun 21 '20 at 07:54
  • It doesn't look like this code implements the skipping, so if you have a version that does, or at least is aimed in that direction, you'll want to edit and update to that version. – tadman Jun 21 '20 at 07:55
  • By "atmost one skip", do you mean skipping at most one character any number of times or skipping any number of characters but only once? – theWiseBro Jun 21 '20 at 07:57
  • For the case if have AABA in string one and AADA the logic should skip atmost one char to find largest common substring. such that it skips B and d and the larges common sub string is AAA – Desmond9989 Jun 21 '20 at 08:01
  • Not sure what you mean by one skip, but to be efficient try using "generalized suffix trees" https://en.wikipedia.org/wiki/Longest_common_substring_problem – gmatht Jun 21 '20 at 09:01
  • Add a `bool` flag `f` into `lcs` to indicate if you have a "skip". Start with `f = false` (`lcs(n, m, 0, false)` in `main()`). For `f = true`, you have the same `lcs` body (no more skips can be introduced). For `f == false`, if `X[i-1] != Y[j-1]`, additionally call `lcs` with `f = true` (drop last characters comparison and introduce a skip). – Evg Jun 21 '20 at 12:47

0 Answers0