-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS (DP).cpp
More file actions
39 lines (35 loc) · 1.14 KB
/
LCS (DP).cpp
File metadata and controls
39 lines (35 loc) · 1.14 KB
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
/// LCS (longest common subsequence) implementation in C language ....
/**
there are (n*m) possible input
each input is processed in O(1) complexity (constant time)
so the total complexity is O(n*m);
**/
#include <stdio.h>
#include <string.h>
const int N = 1234;
int dp[N][N];
char str1[N] , str2[N];
int len1 , len2;
int max (int a , int b) {
return a > b ? a : b;
}
int lcs (int i , int j) {
if (i == len1 || j == len2) return 0; /// jodi index string er baire chole jay taile return 0;
if (dp[i][j] != -1) return dp[i][j]; /// no need to repeat same calculation;
int mx = 0;
mx = max (mx , lcs (i + 1 , j)); /// go through one side;
mx = max (mx , lcs (i , j + 1)); /// go through another side;
if (str1[i] == str2[j]) /// if we find same character between two string;
mx = max (mx , 1 + lcs (i + 1 , j + 1)); /// then go through both side;
dp[i][j] = mx; /// save the maximum value;
return dp[i][j]; /// return the value;
}
int main () {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = -1;
scanf ("%s%s" , &str1 , &str2);
len1 = strlen (str1);
len2 = strlen (str2);
printf ("%d\n" , lcs (0 , 0));
}