Skip to content

Commit 329dc5c

Browse files
Create Longest_Common_Subsequence.c
1 parent 6e67145 commit 329dc5c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Given two strings find the length of the common longest subsequence(need not be contiguous) between the two.
2+
3+
// Example:
4+
5+
// s1: ggtabe
6+
7+
// s2: tgatasb
8+
9+
// The length is 4
10+
11+
12+
#include<stdio.h>
13+
#include<string.h>
14+
int n,m,t1,t2;
15+
int find(char s1[],char s2[],int i,int j,int ans[][m])
16+
{
17+
if(i==n||j==m)
18+
return 0;
19+
else if(s1[i]==s2[j])
20+
return ans[i][j]=1+find(s1,s2,i+1,j+1,ans);
21+
else if(ans[i][j]!=0)
22+
return ans[i][j];
23+
t1=find(s1,s2,i+1,j,ans);
24+
t2=find(s1,s2,i,j+1,ans);
25+
ans[i][j]=(t1>t2)?t1:t2;
26+
return ans[i][j];
27+
}
28+
int main()
29+
{
30+
char s1[10],s2[10];
31+
scanf("%s%s",s1,s2);
32+
n=strlen(s1),m=strlen(s2);
33+
int ans[n][m];
34+
for(int i=0;i<n;i++)
35+
{
36+
for(int j=0;j<m;j++)
37+
ans[i][j]=0;
38+
}
39+
printf("%d",find(s1,s2,0,0,ans));
40+
}

0 commit comments

Comments
 (0)