-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcspatla.cpp
81 lines (73 loc) · 1.52 KB
/
lcspatla.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
int max(int a,int b)
{
return (a>b)?a:b;
}
int commonChild(string s1, string s2){
// Complete this function
int n=s1.length();
int p[2][n];
for(int i=0;i<2;i++)
{
for(int j=0;j<n;j++) //initialization
p[i][j]=0;
}
int i,j=0;
while(j<n)
{
if(j%2==0)
{
if(j==0) //pehla row
{ i=0;
if(s1[i]==s2[i])
p[j][i]=1;
for(i=1;i<n;i++) //next columns
{
if(s1[i]==s2[i])
p[j][i]=1;
else
p[j][i]=p[j][i-1];
} j++;}
else
{
if(s1[0]==s2[j]) p[0][0]=1;
else p[0][0]=p[1][0]; //alternate rows
for(i=1;i<n;i++)
{
if(s1[i]==s2[j])
p[0][i]=p[1][i-1]+1;
else
p[0][i]=max(p[0][i-1],p[1][i]);
}
j++;
}
}
else
{
if(s1[0]==s2[j]) p[1][0]=1;
else p[1][0]=p[0][0];
for(i=1;i<n;i++)
{
if(s1[i]==s2[j])
p[1][i]=p[0][i-1]+1;
else
p[1][i]=max(p[1][i-1],p[0][i]);
}
j++;
}
}
if(n%2!=0)
return p[0][n-1];
else
return p[1][n-1];
}
int main() {
string s1;
cin >> s1;
string s2;
cin >> s2;
int result = commonChild(s1, s2);
cout << result << endl;
return 0;
}