-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdp_longest_common_substring.cpp
More file actions
82 lines (81 loc) · 1.22 KB
/
dp_longest_common_substring.cpp
File metadata and controls
82 lines (81 loc) · 1.22 KB
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" ,stdin);
freopen("output.txt", "w" ,stdout);
#endif
int t;
cin>>t;
while(t--)
{
int m,n;
cin>>m>>n;
string st1,st2;
cin>>st1>>st2;
// cout<<st1<<" "<<st2;
int dp[m][n];
int max = 0;
for(int i=0;i<m;i++)
{
if(st2[0] == st1[i])
{
dp[i][0]=1;
}
else
dp[i][0]=0;
if(max<dp[i][0])
max=dp[i][0];
// cout<<dp[i][0]<<" ";
}
// cout<<endl<<max<<endl;
for(int i=0;i<n;i++)
{
if(st1[0] == st2[i])
{
dp[0][i]=1;
}
else
dp[0][i]=0;
if(max<dp[0][i])
max=dp[0][i];
// cout<<dp[0][i]<<" ";
}
// cout<<st2[1]<<" "<<st1[2];
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(st2[j]==st1[i])
{
// cout<<"hello"<<" ";
dp[i][j] = dp[i-1][j-1]+1;
}
else
{
dp[i][j] = 0;
}
if(max<dp[i][j])
max=dp[i][j];
}
}
cout<<" ";
// for(int i=0;i<n;i++) TO Print the grid
// {
// cout<<st2[i]<<" ";
// }
// cout<<endl;
// for(int i=0;i<m;i++)
// {
// cout<<st1[i]<<" ";
// for(int j=0;j<n;j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
cout<<max;
cout<<endl;
}
}