-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec10_81.cpp
51 lines (38 loc) Β· 1 KB
/
lec10_81.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int longestCommonSubstr(string& s, string& t) {
// your code here
int n = s.size(), m = t.size();
vector<vector<int>> dp(n+1, vector<int>(m+1, 0)); // DP table initialized to 0
// Fill the DP table
int ans = 0 ;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i-1] == t[j-1]) {
dp[i][j] = 1 + dp[i-1][j-1];
ans = max ( ans ,dp[i][j]);}
else {
dp[i][j] = 0; }
}
}
return ans;
}
};
//{ 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;
cout << "~"
<< "\n";
}
}
// } Driver Code Ends