-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.cpp
More file actions
77 lines (70 loc) · 1.14 KB
/
longest_common_subsequence.cpp
File metadata and controls
77 lines (70 loc) · 1.14 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
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
#include "longest_common_subsequence.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
cell m[MAXINT][MAXINT];
void row_init(int i)
{
m[0][i].cost = 0;
}
void column_init(int i)
{
m[i][0].cost = 0;
}
int match(char c, char d)
{
if(c==d) return 0;
else return 1;
}
int indel(char c)
{
return 1;
}
int string_compare(char *s, char *t)
{
int i,j,k;
for (i = 0; i< MAXINT; i++)
{
row_init(i);
column_init(i);
}
for (i=1; i <= strlen(s) ;i++)
{
for (j=1; j <= strlen(t) ;j++)
{
if (s[i] == t[j])
{
m[i][j].parent = MATCH;
m[i][j].cost = m[i-1][j-1].cost + 1;
}
else
{
if (m[i-1][j].cost < m[i][j-1].cost)
{
m[i][j].cost = m[i][j-1].cost;
m[i][j].parent = INSERT;
}
else
{
m[i][j].cost = m[i-1][j].cost;
m[i][j].parent = DELETE;
}
}
}
}
print_lcs(s, i-1, j-1);
return (m[i-1][j-1].cost);
}
void print_lcs(char *s, int i, int j)
{
if(i==0||j==0) return;
if (m[i][j].parent == MATCH)
{
print_lcs(s, i-1, j-1);
printf("%c ", s[i-1]);
}
else if (m[i][j].parent == INSERT)
print_lcs(s, i, j-1);
else if(m[i][j].parent == DELETE)
print_lcs(s, i-1, j);
}