thesonpb

Xâu con chung có độ dài lớn nhất của 2 xâu

Aug 4th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. /* function to find and print the longest common  
  8.    substring of X[0..m-1] and Y[0..n-1] */
  9. void printLCSubStr(char* X, char* Y, int m, int n)
  10. {
  11.     // Create a table to store lengths of longest common
  12.     // suffixes of substrings.   Note that LCSuff[i][j]
  13.     // contains length of longest common suffix of X[0..i-1]
  14.     // and Y[0..j-1]. The first row and first column entries
  15.     // have no logical meaning, they are used only for
  16.     // simplicity of program
  17.     int LCSuff[m + 1][n + 1];
  18.  
  19.     // To store length of the longest common substring
  20.     int len = 0;
  21.  
  22.     // To store the index of the cell which contains the
  23.     // maximum value. This cell's index helps in building
  24.     // up the longest common substring from right to left.
  25.     int row, col;
  26.  
  27.     /* Following steps build LCSuff[m+1][n+1] in bottom
  28.        up fashion. */
  29.     for (int i = 0; i <= m; i++) {
  30.         for (int j = 0; j <= n; j++) {
  31.             if (i == 0 || j == 0)
  32.                 LCSuff[i][j] = 0;
  33.  
  34.             else if (X[i - 1] == Y[j - 1]) {
  35.                 LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;
  36.                 if (len < LCSuff[i][j]) {
  37.                     len = LCSuff[i][j];
  38.                     row = i;
  39.                     col = j;
  40.                 }
  41.             }
  42.             else
  43.                 LCSuff[i][j] = 0;
  44.         }
  45.     }
  46.  
  47.     // if true, then no common substring exists
  48.     if (len == 0) {
  49.         return;
  50.     }
  51.  
  52.     // allocate space for the longest common substring
  53.     char* resultStr = (char*)malloc((len + 1) * sizeof(char));
  54.  
  55.     // traverse up diagonally form the (row, col) cell
  56.     // until LCSuff[row][col] != 0
  57.     resultStr[len] = '\0';
  58.     while (LCSuff[row][col] != 0) {
  59.         resultStr[--len] = X[row - 1]; // or Y[col-1]
  60.  
  61.         // move diagonally up to previous cell
  62.         row--;
  63.         col--;
  64.     }
  65.     // required longest common substring
  66.     cout << resultStr;
  67. }
  68.  
  69. /* Driver program to test above function */
  70. int main()
  71. {
  72.     string x, y;
  73.     cin >> x >> y;
  74.     char* X = new char[x.size()+1]; copy(x.begin(), x.end(), X); X[x.size()] = '\0';
  75.     char* Y = new char[y.size()+1]; copy(y.begin(), y.end(), Y); Y[y.size()] = '\0';
  76.  
  77.     int m = strlen(X);
  78.     int n = strlen(Y);
  79.  
  80.     printLCSubStr(X, Y, m, n);
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment