-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathLCS.cpp
96 lines (72 loc) · 1.51 KB
/
LCS.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
Given 2 strings of S1 and S2 with lengths m and n respectively, find the length of longest common subsequence.
A subsequence of a string S whose length is n, is a string containing characters in same relative order as they are present in S, but not necessarily contiguous.
Subsequences contain all the strings of length varying from 0 to n. E.g. subsequences of string "abc" are - "",a,b,c,ab,bc,ac,abc.
Input Format :
Line 1 : String S1
Line 2 : String s2
Output Format :
Line 1 : Length of lcs
Sample Input :
adebc
dcadb
Sample Output :
3
*/
#include <bits/stdc++.h>
using namespace std;
int lcs(string s1, string s2){
int m = s1.size();
int n = s2.size();
vector<vector<int>> dp(m, vector<int> (n,0));
if (s1[0] == s2[0])
{
dp[0][0] = 1;
}
for (int i = 1; i < n; ++i)
{
if(s1[0] == s2[i])
dp[0][i] = 1;
else
dp[0][i] = dp[0][i-1];
}
for (int i = 1; i < m; ++i)
{
if(s1[i] == s2[0])
dp[i][0] = 1;
else
dp[i][0] = dp[i-1][0];
}
for (int i = 1; i < m; ++i)
{
for (int j = 1; j < n; ++j)
{
if(s1[i] == s2[j])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
// for (int i = 0; i < m; ++i)
// {
// for (int j = 0; j < n; ++j)
// {
// cout << dp[i][j]<<" " ;
// }
// cout <<'\n';
// }
return dp[m-1][n-1];
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
string s1, s2;
cin>>s1>>s2;
cout << lcs(s1, s2) << '\n';
return 0 ;
}