-
Notifications
You must be signed in to change notification settings - Fork 1
/
Longest_commom_substring.cpp
60 lines (60 loc) · 1.22 KB
/
Longest_commom_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
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i,a,n) for(int i=a;i<n;i++)
#define fre(i,a,b) for(int i=a;i<=b;i++)
#define endl '\n'
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define mem(name, value) memset(name, value, sizeof(name))
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define case cout << "Case " << t++ << ": ";
int static dp[1001][1001];
int lcs_substr(string s, string p, int n, int m)
{
int res = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
{
dp[i][j] = 0;
}
else if (s[i - 1] == p[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
res = max(res, dp[i][j]);
}
else {
dp[i][j] = 0;
}
}
}
return res;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
string s;
string p;
cin >> s >> p;
memset(dp , 0, sizeof(dp));
cout << lcs_substr(s, p, n, m) << endl;
}
return 0;
}