StringEdit.java
6.17 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package is2.lemmatizer;
import is2.util.DB;
import java.util.ArrayList;
public class StringEdit {
public static void main(String args[]) {
String s = new StringBuffer(args[0]).reverse().toString();
String t = new StringBuffer(args[1]).reverse().toString();
int d[][] = LD(s, t);
StringBuffer opersations = new StringBuffer();
searchPath(s,t,d, opersations, false);
System.out.println("resuylt "+" "+opersations);
}
//****************************
// Get minimum of three values
//****************************
static private int Minimum (int a, int b, int c) {
int mi;
mi = a;
if (b < mi) mi = b;
if (c < mi) mi = c;
return mi;
}
//*****************************
// Compute Levenshtein distance
//*****************************
static public int[][] LD (String s, String t) {
int n = s.length ();
int m = t.length ();; // length of t
// char s_i; // ith character of s
// char t_j; // jth character of t
int cost; // cost
// Step 1
int[][] d = new int[n+1][m+1];
if (n == 0) return d;
if (m == 0) return d;
// Step 2
for (int i = 0; i <= n; i++) d[i][0] = i;
for (int j = 0; j <= m; j++) d[0][j] = j;
// Step 3
for (int i = 1; i <= n; i++) {
int s_i = s.charAt (i - 1);
// Step 4
for (int j = 1; j <= m; j++) {
// t_j = t.charAt (j - 1);
// Step 5
if (s_i == t.charAt (j - 1)) cost = 0;
else cost = 1;
// Step 6
d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);
}
}
// Step 7
return d;
}
static String searchPath(String s, String t, int[][] d, StringBuffer operations, boolean debug) {
StringBuffer result = new StringBuffer(s);
int n = d.length;
int m = d[0].length;
int x=n-1;
int y=m-1;
boolean changed =false;
while(true) {
if (debug && changed )System.out.println("result "+new StringBuffer(result) .reverse());
if (d[x][y]==0)break;
if (y>0&&x>0&& d[x-1][y-1]<d[x][y]) {
if (debug) System.out.println("min d[x-1][y-1] "+d[x-1][y-1]+" d[x][y] "+d[x][y]+" rep "+s.charAt(x-1)+" with "+t.charAt(y-1)+" at "+(x-1));
operations.append('R').append(Character.toString((char)((int)x-1))).append(s.charAt(x-1)).append(t.charAt(y-1));
if (debug) result.setCharAt(x-1, t.charAt(y-1));
y--;
x--;
changed =true;
continue;
}
if (y>0&& d[x][y-1]<d[x][y]) {
if (debug) System.out.println("min d[x][y-1] "+d[x][y-1]+" d[x][y] "+d[x][y]+" ins "+t.charAt(y-1)+" at "+(x));
operations.append('I').append(Character.toString((char)((int)x))).append(t.charAt(y-1));
if (debug)result.insert(x, t.charAt(y-1));
y--;
changed =true;
continue;
}
if (x>0&& d[x-1][y]<d[x][y]) {
if (debug)System.out.println("min d[x-1][y] "+d[x-1][y]+" d[x][y] "+d[x][y]+" del "+s.charAt(x-1)+" at "+(x-1));
operations.append('D').append(Character.toString((char)((int)x-1))).append(s.charAt(x-1));
if (debug)result.deleteCharAt(x-1);
x--;
changed =true;
continue;
}
changed =false;
if (x>0&& y>0 && d[x-1][y-1]==d[x][y]) {
x--; y--;
continue ;
}
if (x>0&& d[x-1][y]==d[x][y]) {
x--;
continue;
}
if (y>0 && d[x][y-1]==d[x][y]) {
y--;
continue;
}
}
if (debug) return result.reverse().toString();
else return null;
}
public static String change(String s, String operations) {
StringBuffer result = new StringBuffer(s).reverse();
int pc =0;
while(true) {
if (operations.length()<=pc) break;
char nextOperation = operations.charAt(pc);
pc++;
if (nextOperation == 'R') {
//pc++;
int xm1 = (char)operations.charAt(pc);
pc++;
char replace = operations.charAt(pc);
pc++;
char with = operations.charAt(pc);
//operations.append('R').append((char)x-1).append(s.charAt(x-1)).append(t.charAt(y-1));
// System.out.println(""+result+" xm1 "+xm1+" op "+operations);
if (result.length()<=xm1) return s;
if (result.charAt(xm1)==replace) result.setCharAt(xm1, with);
//if (debug) result.setCharAt(x-1, t.charAt(y-1));
pc++;
}else if (nextOperation == 'I') {
// if (debug) System.out.println("min d[x][y-1] "+d[x][y-1]+" d[x][y] "+d[x][y]+" ins "+t.charAt(y-1)+" at "+(x));
//operations.append('I').append((char)x).append(t.charAt(y-1));
//if (debug)result.insert(x, t.charAt(y-1));
//y--;
//changed =true;
//pc++;
int x = operations.charAt(pc);
pc++;
char in = operations.charAt(pc);
if (result.length()<x) return s;
result.insert(x, in);
pc++;
} else if (nextOperation == 'D' ) {
//pc++;
int xm1 = operations.charAt(pc);
if (result.length()<=xm1) return s;
result.deleteCharAt(xm1);
pc++;
// delete with
pc++;
// operations.append('D').append((char)x-1).append(s.charAt(x-1));
// if (debug)result.deleteCharAt(x-1);
}
}
return result.reverse().toString();
//else return null;
}
/**
* @param opers
* @param postion
* @return
*/
public static String get(ArrayList<String> opers, int position) {
for(String s : opers) {
int p = (int)s.charAt(1);
if (p==position) {
return s;
}
}
return "0";
}
/**
* @param form
* @param string
* @param c
* @return
*/
public static String changeSimple(String form, String operation, int c) {
if (operation.equals("0")) return form;
if (operation.charAt(0)=='I') {
StringBuffer f = new StringBuffer(form);
if (f.length()<=c) {
// DB.println("fail insert ");
return form;
}
f.insert(c+1, operation.charAt(1));
return f.toString();
}
if (operation.charAt(0)=='R') {
StringBuffer f = new StringBuffer(form);
// if (f.length()<=c) f.append(' ');
if (f.length()<=c) {
// DB.println("fail replace ");
return form;
}
f.setCharAt(c, operation.charAt(2));
return f.toString();
}
if (operation.charAt(0)=='D') {
StringBuffer f = new StringBuffer(form);
f.delete(c, c+1);//.append(' ');
return f.toString();
}
return form;
}
/**
* @param string
* @return
*/
public static String simple(String o) {
StringBuffer s = new StringBuffer(o);
s.delete(1, 2);
return s.toString();
}
}