Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int longestCommonSubstring(string &A, string &B) {
- int LCSuff[A.length()+1][B.length()+1];
- int result = 0; // To store length of the longest common substring
- /* Following steps build LCSuff[m+1][n+1] in bottom up fashion. */
- for (int i=0; i<=A.length(); i++) {
- for (int j=0; j<=B.length(); j++) {
- if (i == 0 || j == 0) {
- LCSuff[i][j] = 0;
- } else if (A[i-1] == B[j-1]) {
- LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
- result = max(result, LCSuff[i][j]);
- } else {
- LCSuff[i][j] = 0;
- }
- }
- }
- return result;
- }
Add Comment
Please, Sign In to add comment