-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path134.java
More file actions
172 lines (146 loc) · 5.01 KB
/
Copy path134.java
File metadata and controls
172 lines (146 loc) · 5.01 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.zap.utils;
/**
* This class provides the implementation of Hirschberg's
* algorithm that solves the Longest Common Subsequence (LCS) problem.
* With some shortcut methods useful to calculate LCS between two strings
* or the similarity ratio between two strings.
* This implementation is using Hirschber's algorithm B and algorithm C.
*
* @see <a href="https://code.google.com/archive/p/algorithm800/source/default/source">Hirschberg's algorithm implementation of
* project algorithm800</a>
*
* @author Valentinos Georgiades
* @author Minh Nguyen
*/
//ZAP
public class HirshbergMatcher {
// Minimum value for comparison ratio
public static final double MIN_RATIO = 0.0;
// Maximum value for comparison ratio
public static final double MAX_RATIO = 1.0;
private static int[] algB(int m, int n, String a, String b) {
// Step 1
int[][] k = new int[2][n + 1];
for (int j = 0; j <= n; j++) {
k[1][j] = 0;
}
// Step 2
for (int i = 1; i <= m; i++) {
// Step 3
for (int j = 0; j <= n; j++) {
k[0][j] = k[1][j];
}
// Step 4
for (int j = 1; j <= n; j++) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
k[1][j] = k[0][j - 1] + 1;
} else {
k[1][j] = Math.max(k[1][j - 1], k[0][j]);
}
}
}
//Step 5
return k[1];
}
private static void algC(StringBuilder sb, int m, int n, String a, String b) {
int i;
int j;
// Step 1
if (n == 0) {
// Nothing to do
} else if (m == 1) {
for (j = 0; j < n; j++) {
if (a.charAt(0) == b.charAt(j)) {
sb.append(a.charAt(0));
break;
}
}
// Step 2
} else {
i = (int)Math.floor(((double) m) / 2);
// Step 3
int[] l1 = algB(i, n, a.substring(0, i), b);
int[] l2 = algB(m - i, n, reverseString(a.substring(i)), reverseString(b));
// Step 4
int k = findK(l1, l2, n);
// Step 5
algC(sb, i, k, a.substring(0, i), b.substring(0, k));
algC(sb, m - i, n - k, a.substring(i), b.substring(k));
}
}
/**
* This method takes a string as input reverses it and returns the result
*
* @param in the string to be reversed
* @return the reversed string
*/
private static String reverseString(String in) {
StringBuilder out = new StringBuilder(in).reverse();
return out.toString();
}
private static int findK(int[] l1, int[] l2, int n) {
int m = 0;
int k = 0;
for (int j = 0; j <= n; j++) {
if (m < (l1[j] + l2[n - j])) {
m = l1[j] + l2[n - j];
k = j;
}
}
return k;
}
/**
* Gets the Longest Common Subsequence of two strings, using Dynamic
* programming techniques, and minimal memory
*
* @param strA the first String
* @param strB the second String
* @return the Longest Common Subsequence of strA and strB
*/
public String getLCS(String strA, String strB) {
if ("".equals(strA)) {
return "";
}
StringBuilder sb = new StringBuilder();
algC(sb, strA.length(), strB.length(), strA, strB);
return sb.toString();
}
/**
* Calculate the ratio of similarity between 2 strings using LCS
*
* @param strA the first String
* @param strB the second String
* @return the percentage double number
*/
public double getMatchRatio(String strA, String strB) {
if (strA == null && strB == null) {
return MAX_RATIO;
} else if (strA == null || strB == null) {
return MIN_RATIO;
}
if (strA.isEmpty() && strB.isEmpty()) {
return MAX_RATIO;
} else if (strA.isEmpty() || strB.isEmpty()) {
return MIN_RATIO;
}
//get the percentage match against the longer of the 2 strings
return (double)getLCS(strA, strB).length() / Math.max(strA.length(), strB.length());
}
}