-
Notifications
You must be signed in to change notification settings - Fork 62
/
LCS
56 lines (37 loc) · 736 Bytes
/
LCS
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
#include<bits/stdc++.h>
using namespace std;
int n1,n2;
string s1,s2;
int fun(int i,int j,int len)
{
if(i==n1 || j==n2)
return len;
if(s1[i]==s2[j])
return fun(i+1,j+1,len+1);
return max(fun(i+1,j,len),fun(i,j+1,len));
}
int fun_dp()
{
int dp[n1+1][n2+1];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n2;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=1+dp[i-1][j-1];
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
return dp[n1][n2];
}
int main() {
int t; cin>>t;
while(t--)
{
cin>>n1>>n2>>s1>>s2;
cout<<fun_dp()<<endl;
}
return 0;
}