Advertisement
asdfg0998

Untitled

Apr 1st, 2025
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. int longestSubsequence(string x, string y) {
  2.     int n = x.size(), m = y.size();
  3.     vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  4.  
  5.     // Compute the longest common subsequence that is also a substring
  6.     int maxLength = 0;
  7.     for (int i = 1; i <= n; i++) {
  8.         for (int j = 1; j <= m; j++) {
  9.             if (x[i - 1] == y[j - 1]) {
  10.                 dp[i][j] = dp[i - 1][j - 1] + 1;
  11.                 maxLength = max(maxLength, dp[i][j]);
  12.             }
  13.         }
  14.     }
  15.  
  16.     return maxLength;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement