-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubstring-search.cpp
87 lines (71 loc) · 1.49 KB
/
substring-search.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
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
/**
* @file substring-search.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-17
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX_SIZE 100
#define MATCH 0
#define INSERT 1
#define DELETE 2
typedef struct {
int cost;
int parent;
} cell;
int substring_matching(string T, string P) {
int n = T.length();
int m = P.length();
int i, j, k;
cell dp[n + 1][m + 1];
int opt[3];
int lowest_cost;
// row init
for (i = 0; i <= n; i++) {
dp[i][0].cost = 0;
dp[i][0].parent = -1;
}
// column init
for (j = 0; j <= m; j++) {
dp[0][j].cost = 0;
dp[0][j].parent = -1;
}
// fill dp
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
opt[MATCH] = dp[i - 1][j - 1].cost + (T[i - 1] == P[j - 1] ? 0 : 1);
opt[INSERT] = dp[i][j-1].cost + 1;
opt[DELETE] = dp[i-1][j ].cost + 1;
dp[i][j].cost = opt[MATCH];
dp[i][j].parent = MATCH;
for(k=INSERT;k<=DELETE;k++){
if(opt[k]<dp[i][j].cost){
dp[i][j].cost = opt[k];
dp[i][j].parent = k;
}
}
}
}
// goal cell
i = T.length() ;
j = 0;
for (k = 1; k <= P.length(); k++) {
if (dp[i][k].cost < dp[i][j].cost) {
j = k;
}
}
return dp[i][j].cost;
}
int main(int argc, const char **argv) {
string T = "text";
string P = "ext";
cout<< substring_matching(T,P)<<endl;
return 0;
}