-
Notifications
You must be signed in to change notification settings - Fork 0
/
14Longest Common Substring.cpp
53 lines (38 loc) · 1.15 KB
/
14Longest Common Substring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int solve(string &str1, string &str2, int i, int j, vector<vector<int>>& dp, int &maxi) {
if (i >= str1.length() || j >= str2.length()) return 0;
if (dp[i][j] != -1) return dp[i][j];
int curr = 0;
if (str1[i] == str2[j]) {
curr = 1 + solve(str1, str2, i + 1, j + 1, dp, maxi);
maxi = max(maxi, curr);
}
dp[i][j] = curr;
solve(str1, str2, i, j + 1, dp, maxi);
solve(str1, str2, i + 1, j, dp, maxi);
return dp[i][j];
}
int longestCommonSubstr(string str1, string str2) {
int maxi = 0;
vector<vector<int>> dp(str1.length(), vector<int>(str2.length(), -1));
solve(str1, str2, 0, 0, dp, maxi);
return maxi;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string s1, s2;
cin >> s1 >> s2;
Solution ob;
cout << ob.longestCommonSubstr(s1, s2) << endl;
}
}
// } Driver Code Ends