Skip to content

Instantly share code, notes, and snippets.

@tinwritescode
Last active December 24, 2019 16:53
Show Gist options
  • Save tinwritescode/a2c7cda5c0264f820994e3ad2abdc6bc to your computer and use it in GitHub Desktop.
Save tinwritescode/a2c7cda5c0264f820994e3ad2abdc6bc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
int max(int a, int b)
{
return a > b ? a : b;
}
string LCSubStr(string X, string Y, int m, int n)
{
short LCSuff[256][256];
string result = "";
int max = 0;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) LCSuff[i][j] = 0;
else if (X[i - 1] == Y[j - 1]) {
LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;
if (max < LCSuff[i][j]) {
max = LCSuff[i][j];
result = X.substr(i - LCSuff[i][j], max);
}
}
else LCSuff[i][j] = 0;
}
}
return result;
}
/* Driver program to test above function */
int main()
{
string X = "OldSite:GeeksforGeeks.org";
string Y = "NewSite:GeeksQuiz.com";
const int m = X.length();
const int n = Y.length();
cout << "Length of Longest Common Substring is "
<< LCSubStr(X, Y, m, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment