-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Common_Subsequence_two_seq.c
More file actions
41 lines (38 loc) · 1.03 KB
/
Copy pathLongest_Common_Subsequence_two_seq.c
File metadata and controls
41 lines (38 loc) · 1.03 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
40
41
#include<stdio.h>
#include<stdlib.h>
void long_common_sub2(int*, int, int*, int, int**);
void long_common_sub2(int* arr1, int n1, int* arr2, int n2, int** matrix)
{
matrix = (int**)calloc(n1, sizeof(int*));
int i = 0, j = 0;
for(; i < n1; i++)
{
matrix[i] = (int*)calloc(n2, sizeof(int));
matrix[i][0] = 0;
}
for(i = 0; i < n2; i++) matrix[0][i] = 0;
for(i = 1; i < n1; i++)
{
for(j = 1; j < n2; j++)
{
if(arr1[i - 1] != arr2[j - 1]) matrix[i][j] = ((matrix[i - 1][j] > matrix[i][j - 1]) ? matrix[i - 1][j] : matrix[i][j - 1]);
else matrix[i][j] = matrix[i - 1][j - 1] + 1;
}
}
printf("%d", matrix[i - 1][j - 1]);
}
void main()
{
int n1, n2, i = 0;
scanf("%d", &n1);
int* arr1 = (int*)calloc(n1, sizeof(int));
for(i = 0; i < n1; i++) scanf("%d", &arr1[i]);
scanf("%d", &n2);
int* arr2 = (int*)calloc(n2, sizeof(int));
for(i = 0; i < n2; i++) scanf("%d", &arr2[i]);
int** matrix;
long_common_sub2(arr1, n1 + 1, arr2, n2 + 1, matrix);
free(arr1);
free(arr2);
free(matrix);
}