-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8960 - Longest Common Sub-sequence.c
163 lines (126 loc) · 5.03 KB
/
8960 - Longest Common Sub-sequence.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**********************************************************************************************************
NAME : CANDIDA RUTH NORONHA
CLASS : SE COMPS B
ROLL NO. : 8960
BATCH : C
TITLE : LONGEST COMMON SUB-SEQUENCE
SUBMISSION DATE : 12th APRIL, 2021
**********************************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int c[10][10]={0},b[10][10]={0};
// initializing length array (c array) and direction array (b-array) to 0
char x[10]={0},y[10]={0},sub[10]={0};
/* initializing first string(i.e.x),second string (i.e.y) and the subsequence string(i.e.sub) to zero */
int i,j,m,n,len=0; //length variable getting initialized to zero
int main()
{
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n\t\t\t\t Longest Common Sub-Sequence\n");
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Enter the 1st string : ");
scanf("%s",x);
printf("\n Enter the 2nd string : ");
scanf("%s",y); // accepting the second string from the user
printf("\n------------------------------------------------------------------------------------------------\n");
m=strlen(x); // storing the length of first string in m variable
n=strlen(y); // storing the length of second string in n variable
for(i=1;i<=m;i++) // i is the loop variable used for the 1st string
{
for(j=1;j<=n;j++) // j is the loop variable used for the 2nd string
{
if(x[i-1]==y[j-1]) //last character of two strings are compared for equality
{
c[i][j]=c[i-1][j-1]+1; // look at the value in diagonal direction and increment it by 1
b[i][j]=1;//set direction as diagonal
}
else
{
if(c[i-1][j]>=c[i][j-1]) /* checks and stores that value in c array and assigns the corresponding direction to b-array */
{
c[i][j]=c[i-1][j];
b[i][j]=2; //Top Box i.e. vertical direction
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=3; //Horizontal Box i.e. horizontal direction
}
}
}
}
printf("\n\t ***** C-array *****\n\n");
for(i=0;i<=m;i++) //loop for printing the length array
{
for(j=0;j<=n;j++)
{
printf(" %d\t",c[i][j]);
}
printf("\n\n");
}
printf("\n\t ***** B-array *****\n\n");
for(i=0;i<=m;i++) // loop for printing the direction array
{
for(j=0;j<=n;j++)
{
printf(" %d\t",b[i][j]);
}
printf("\n\n");
}
printf("\n\t\tHere,\n\t\t1 => Diagonal\n\t\t2 => Top\n\t\t3 => Horizontal\n"); // will print the directional information for the user's convenience
while(1) //
{
if(b[m][n]==0) //when first column or first row is reached ( empty string)
break;
else if(b[m][n]==1) //letters at diagonal direction will give us the final subsequence
{
sub[len]=x[m-1];
m=m-1;
n=n-1;
len++;
}
else if(b[m][n]==2) // if vertical direction
{
m=m-1; // go the the previous row
}
else if(b[m][n]==3) // if horizontal direction
{
n=n-1; // go to the previous column
}
}
printf("\n------------------------------------------------------------------------------------------------\n");
printf("\n Longest Common Sub-Sequence : ");
for(i=len-1;i>=0;i--) //loop for printing the subsequence
printf("%c",sub[i]);
printf("\n\n------------------------------------------------------------------------------------------------\n");
return 0;
}
/**********************************************************************************************************
OUTPUT:
------------------------------------------------------------------------------------------------
Longest Common Sub-Sequence
------------------------------------------------------------------------------------------------
Enter the 1st string : abcb
Enter the 2nd string : bdcab
------------------------------------------------------------------------------------------------
***** C-array *****
0 0 0 0 0 0
0 0 0 0 1 1
0 1 1 1 1 2
0 1 1 2 2 2
0 1 1 2 2 3
***** B-array *****
0 0 0 0 0 0
0 2 2 2 1 3
0 1 3 3 2 1
0 2 2 1 3 2
0 1 2 2 2 1
Here,
1 => Diagonal
2 => Top
3 => Horizontal
------------------------------------------------------------------------------------------------
Longest Common Sub-Sequence : bcb
-----------------------------------------------------------------------------------------------
**********************************************************************************************************/