-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.cpp
More file actions
61 lines (53 loc) · 987 Bytes
/
lcs.cpp
File metadata and controls
61 lines (53 loc) · 987 Bytes
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
#include<bits/stdc++.h>
using namespace std;
string str1;
string str2;
int CS[100][100];
int LCS(int i, int j)
{
if(str1[i] == '\0' || str2[j] == '\0')
{
return 0;
}
else if(CS[i][j] != -1)
{
return CS[i][j];
}
else if(str1[i] == str2[j])
{
CS[i][j] = 1 + LCS(i+1,j+1);
return CS[i][j];
}
else
{
CS[i][j] = max(LCS(i+1,j),LCS(i,j+1));
return CS[i][j];
}
}
int main()
{
memset(CS,-1,sizeof(CS));
cout<<"Enter First String"<<endl;
cin>>str1;
cout<<"Enter Second String"<<endl;
cin>>str2;
//
// int a =0, b = 0;
// while (str1[a] != '\0')
// {
// ++a;
// }
// while (str2[b] != '\0')
// {
// ++b;
// }
for (int i=0; i<=100; i++)
{
for (int j=0; j<=100; j++)
{
CS[i][j] = -1;
}
}
int r = LCS(0,0);
cout<<r;
}