diff --git a/C/Algorithms/Dynamic-Programming/Longest_Common_Subsequence.c b/C/Algorithms/Dynamic-Programming/Longest_Common_Subsequence.c new file mode 100644 index 00000000..cc540614 --- /dev/null +++ b/C/Algorithms/Dynamic-Programming/Longest_Common_Subsequence.c @@ -0,0 +1,47 @@ +#include +#include +#include + +int main() +{ + int l1,l2,i,j,check=0; + char *ar1=(char*)malloc(50*sizeof(char)); + char *ar2=(char*)malloc(50*sizeof(char)); + printf("enter string 1 and string 2\n"); + scanf("%s %s",ar1,ar2); + l1=strlen(ar1); + l2=strlen(ar2); + + //CREATING DP TABLE + int **arr=(int **)malloc((l1+1)*sizeof(int *));//2D array with constitutes the DP table + for(i=0;i<=l1;i++) + arr[i]=(int *)malloc((l2+1)*sizeof(int *)); + + //PREPROCESSING DP TABLE + for(i=0;i<=l1;i++) + arr[i][0]=0; + for(i=0;i<=l2;i++) + arr[0][i]=0; + + //DP ITERATION STARTS + for(i=1;i<=l1;i++) + { + for(j=1;j<=l2;j++) + { + if(ar1[i-1]==ar2[j-1]) + arr[i][j]=arr[i-1][j-1]+1; + else + { + if(arr[i-1][j]>arr[i][j-1]) + arr[i][j]=arr[i-1][j]; + else + arr[i][j]=arr[i][j-1]; + } + } + } + //arr[l1][l2] contains the length of the longest common subsequence + printf("The length of Longest Common Subsequence is %d\n", arr[l1][l2]); + + return 0; +} + diff --git a/C/Algorithms/Dynamic-Programming/readme.md b/C/Algorithms/Dynamic-Programming/readme.md index b4e1e362..2f5388d2 100644 --- a/C/Algorithms/Dynamic-Programming/readme.md +++ b/C/Algorithms/Dynamic-Programming/readme.md @@ -1 +1,6 @@ ### Dynamic-Programming +- [Longest Common Subsequence](Longest_Common_Subsequence.c) + + Given two sequences, find the length of longest subsequence present in both of them. + + Time complexity = O(mn) where "m" is the length of the 1st sequence and "n" is the length of the 2nd sequence