Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- /* function to find and print the longest common
- substring of X[0..m-1] and Y[0..n-1] */
- void printLCSubStr(char* X, char* Y, int m, int n)
- {
- // Create a table to store lengths of longest common
- // suffixes of substrings. Note that LCSuff[i][j]
- // contains length of longest common suffix of X[0..i-1]
- // and Y[0..j-1]. The first row and first column entries
- // have no logical meaning, they are used only for
- // simplicity of program
- int LCSuff[m + 1][n + 1];
- // To store length of the longest common substring
- int len = 0;
- // To store the index of the cell which contains the
- // maximum value. This cell's index helps in building
- // up the longest common substring from right to left.
- int row, col;
- /* Following steps build LCSuff[m+1][n+1] in bottom
- up fashion. */
- 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 (len < LCSuff[i][j]) {
- len = LCSuff[i][j];
- row = i;
- col = j;
- }
- }
- else
- LCSuff[i][j] = 0;
- }
- }
- // if true, then no common substring exists
- if (len == 0) {
- return;
- }
- // allocate space for the longest common substring
- char* resultStr = (char*)malloc((len + 1) * sizeof(char));
- // traverse up diagonally form the (row, col) cell
- // until LCSuff[row][col] != 0
- resultStr[len] = '\0';
- while (LCSuff[row][col] != 0) {
- resultStr[--len] = X[row - 1]; // or Y[col-1]
- // move diagonally up to previous cell
- row--;
- col--;
- }
- // required longest common substring
- cout << resultStr;
- }
- /* Driver program to test above function */
- int main()
- {
- string x, y;
- cin >> x >> y;
- char* X = new char[x.size()+1]; copy(x.begin(), x.end(), X); X[x.size()] = '\0';
- char* Y = new char[y.size()+1]; copy(y.begin(), y.end(), Y); Y[y.size()] = '\0';
- int m = strlen(X);
- int n = strlen(Y);
- printLCSubStr(X, Y, m, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment