Forum >> Programmazione Python >> Web e Reti >> Problem with Longest Common Substring

Pagina: 1

I'm having difficulty understanding the code for the Longest Common Substring problem on my regular resource. I'm not getting the desired output when I try to run the code. Here is the code I'm trying to use:

// Function to find Longest Common Substring

// of two given strings

string LCS(string X, string Y)

{

// Find lengths of given strings

intm = X.length ();

int n = Y.length();




// Declare the 2D array

int L[m+1][n+1];




//Fill values ​​​​in the array

for (int i = 0; i <= m; i++)

{

for (int j = 0; j <= n; j++)

{

if (i == 0 || j == 0)

Lj = 0;

else if (X[i - 1] == Y[j - 1])

L j = L[i - 1][j - 1] + 1;

else

L j = max(L[i - 1]j, L [j - 1]);

}

}




// Backtrack to find the substring

int index = Lmn;




// Create a string length index+1

string lcs(index+1, ' ');




// Start from the right-most-bottom-most corner

// and one by one store characters in lcs[]

int i = m, j = n;

while (i > 0 && j > 0)

{

// If current character in X[] and Y are same

if (X[i - 1] == Y[j - 1])

{

// Put current character into result

lcs [index - 1] = X[i - 1];




// reduce values ​​of i, j and index

the--;

j--;

index--;

}




// If not same, then find the larger of two and

// go in the direction of larger values

​​else if (L[i - 1]j > L [j - 1])

the--;

else

j--;

}




// Return the result

return lcs;

}
I'm hoping someone can help me figure out what's wrong with the code and help me get the desired output. Thanks in advance!


--- Ultima modifica di Rosaline in data 2023-04-19 14:08:33 ---


Pagina: 1



Esegui il login per scrivere una risposta.