-
Notifications
You must be signed in to change notification settings - Fork 62
/
LCS OF THREE STRINGS
54 lines (36 loc) · 950 Bytes
/
LCS OF THREE STRINGS
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
#include <bits/stdc++.h>
using namespace std;
int n,m,k;
string s1,s2,s3;
int fun_dp()
{
int dp[n+1][m+1][k+1];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int p=1;p<=k;p++)
{
if(s1[i-1]==s2[j-1]&& s2[j-1]==s3[p-1] && s1[i-1]==s3[p-1])
dp[i][j][p]=1+dp[i-1][j-1][p-1];
else
{
int max1=max(dp[i-1][j][p],max(dp[i][j-1][p],dp[i][j][p-1]));
int max2=max(dp[i-1][j-1][p],max(dp[i][j-1][p-1],dp[i-1][j][p-1]));
dp[i][j][p]=max(max1,max2);
}
}
}
}
return dp[n][m][k];
}
int main() {
int t; cin>>t;
while(t--)
{
cin>>n>>m>>k>>s1>>s2>>s3;
cout<<fun_dp()<<endl;
}
return 0;
}