-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl_c_s.cpp
50 lines (45 loc) · 1.04 KB
/
l_c_s.cpp
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
/*длина наибольшей общей подпоследовательности*/
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <iostream>
using namespace std;
int max(int a, int b)
{
return (a > b) ? a : b;
}
int l_s_c(char *x, char *y, int m, int n)
{
int **mat = new int*[m + 1];
for (int j = 0; j < m + 1; j++)
mat[j] = new int[n + 1];
int i, j;
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
mat[i][j] = 0;
else if (x[i - 1] == y[j - 1])
mat[i][j] = mat[i - 1][j - 1] + 1;
else
mat[i][j] = max(mat[i][j - 1], mat[i - 1][j]);
}
}
for (i = 0; i <= m; i++)
{
for (j = 0; j <= n; j++)
cout << mat[i][j] << " ";
cout << endl;
}
return mat[m][n];
}
int main(int argc, char *argv[])
{
char x[] = "agcat";//"XMJYAUZ";
char y[] = "gac";//"MZJAWXU";
int m = strlen(x);
int n = strlen(y);
cout << "Length of the longest common subsequence = " << l_s_c(x, y, m, n) << endl;
system("pause");
return EXIT_SUCCESS;
}