-
Notifications
You must be signed in to change notification settings - Fork 18
/
lcs_substring
92 lines (81 loc) · 1.6 KB
/
lcs_substring
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Code Written by hritikhrk (Hritik Kumar)
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define F first
#define S Second
#define VI vector<int>
#define VLL vector<long long>
#define PII pair<int, int>
#define MII map<int, int>
#define pb push_back()
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
const int MOD = 1e9 + 7;
inline ll mod(ll x, ll m)
{
return (x % m + m) % m;
}
int lcSubstring(string x, string y, int n, int m)
{
int dp[n + 1][m + 1];
for (int i = 0; i < n + 1; i++)
{
dp[i][0] = 0;
}
for (int i = 0; i < m + 1; i++)
{
dp[0][i] = 0;
}
int result = 0;
for (int i = 1; i < n + 1; i++)
{
for (int j = 1; j < m + 1; j++)
{
if (x[i - 1] == y[j - 1])
{
dp[i][j] = 1 + dp[i - 1][j - 1];
result = max(result, dp[i][j]);
}
else
dp[i][j] = 0;
}
}
return result;
}
void solve()
{
string x, y;
cin >> x >> y;
int n = x.length();
int m = y.length();
cout << lcSubstring(x, y, n, m) << endl; //dp
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
//Code starts from here //
int testCases;
cin >> testCases;
while (testCases--)
solve();
// Code end here //
return 0;
}
// INPUT:
// abcde
// abfce
// OUTPUT:
// 2