-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsubstringedit.c
127 lines (91 loc) · 2.6 KB
/
substringedit.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
/* substringedit.c
Approximately match one string as a substring of another, where is s in t?
by: Steven Skiena
begun: March 26, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <string.h>
#include "bool.h"
#include "editdistance.h"
cell m[MAXLEN+1][MAXLEN+1]; /* dynamic programming table */
/******************************************************************/
/* For normal edit distance computation */
/* [[[ goal_cell_288_cut */
void goal_cell(char *s, char *t, int *i, int *j) {
int k; /* counter */
*i = strlen(s) - 1;
*j = 0;
for (k = 1; k < strlen(t); k++) {
if (m[*i][k].cost < m[*i][*j].cost) {
*j = k;
}
}
}
/* ]]] */
/* [[[ match_288_cut */
int match(char c, char d) {
if (c == d) {
return(0);
}
return(1);
}
/* ]]] */
int indel(char c) {
return(1);
}
/* [[[ row_init_288_cut */
void row_init(int i, cell m[MAXLEN+1][MAXLEN+1]) { /* what is m[0][i]? */
m[0][i].cost = 0; /* NOTE CHANGE */
m[0][i].parent = -1; /* NOTE CHANGE */
}
/* ]]] */
void column_init(int i, cell m[MAXLEN+1][MAXLEN+1]) { /* what is m[i][0]? */
m[i][0].cost = i;
if (i > 0) {
m[i][0].parent = DELETE;
} else {
m[0][i].parent = -1;
}
}
/**********************************************************************/
void match_out(char *s, char *t, int i, int j) {
if (s[i] == t[j]) {
printf("M");
} else {
printf("S");
}
}
void insert_out(char *t, int j) {
printf("I");
}
void delete_out(char *s, int i) {
printf("D");
}
/**********************************************************************/
int main(void) {
int i, j;
char s[MAXLEN], t[MAXLEN]; /* input strings */
s[0] = t[0] = ' ';
scanf("%s", &(s[1]));
scanf("%s", &(t[1]));
printf("matching cost = %d \n", string_compare(s, t, m));
print_matrix(s, t, TRUE, m);
printf("\n");
print_matrix(s, t, FALSE, m);
goal_cell(s, t, &i, &j);
printf("%d %d\n", i, j);
reconstruct_path(s, t, i, j, m);
printf("\n");
return 0;
}