Created
November 6, 2017 20:49
-
-
Save bexp/43a2f81a85821805814847600a648543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int longest_substring(const string& str1, const string& str2) { | |
int n = str1.length(); | |
int m = str2.length(); | |
int suffix[n + 1][m + 1] = {}; | |
int result = 0; | |
for (int i = 0; i <= n;i++) { | |
for (int j = 0; j <=m; j++) { | |
if (i == 0 || j == 0) { | |
suffix[i][j] = 0; | |
} else if (str1.at(i - 1) == str2.at(j - 1)) { | |
suffix[i][j] = suffix[i - 1][j - 1] + 1; | |
result = max(result, suffix[i][j]); | |
} else { | |
suffix[i][j] = 0; | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment