-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.c
More file actions
66 lines (61 loc) · 981 Bytes
/
lcs.c
File metadata and controls
66 lines (61 loc) · 981 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
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int max(int a,int b)
{
if(a>=b)
return a;
else
return b;
}
void lcs(char *x,char *y,int m,int n)
{
int arr[n+1][m+1];int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i==0||j==0)
arr[i][j]=0;
else if(x[j-1]==y[i-1])
arr[i][j]=arr[i-1][j-1]+1;
else
arr[i][j]=max(arr[i-1][j],arr[i][j-1]);
}
}
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("Length of the lcs is:%d\n",arr[n][m]);
int ind=arr[n][m];
char lcs[ind+1];
lcs[ind]='\0';
i=n;j=m;
while(i>0&&j>0)
{
if(x[j-1]==y[i-1])
{ lcs[ind-1]=x[j-1];
i--;j--;ind--;
}
else if(arr[i-1][j]>arr[i][j-1])
i--;
else
j--;
}
printf("LCS:%s\n",lcs);
}
int main()
{
char *x,*y;
x=(char *)malloc(10*sizeof(char));
y=(char *)malloc(10*sizeof(char));
printf("Enter the strings:");
gets(x);gets(y);
lcs(x,y,strlen(x),strlen(y));
return 0;
}