Advertisement
keverman

Longest Common Substring

May 13th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. std::string LCstr(std::string &A, std::string &B)
  2. {
  3.     std::vector<std::vector<int>> DP(A.size() + 1, std::vector<int>(B.size() + 1, 0));
  4.     int pos = 0, mx = 0;
  5.  
  6.     for(int i = 1; i <= A.size(); i++)
  7.     {
  8.         for(int j = 1; j <= B.size(); j++)
  9.         {
  10.             if(A[i - 1] == B[j - 1])
  11.             {
  12.                 DP[i][j] = DP[i - 1][j - 1] + 1;
  13.  
  14.                 if(DP[i][j] > mx)
  15.                 {
  16.                     mx = DP[i][j];
  17.                     pos = i - 1;
  18.                 }
  19.             }
  20.         }
  21.     }
  22.  
  23.     return A.substr(pos - mx + 1, mx);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement