-
Notifications
You must be signed in to change notification settings - Fork 5
/
EditDistance.java
executable file
·60 lines (54 loc) · 1.41 KB
/
EditDistance.java
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
package com.vee.algorithms.dynprog;
public class EditDistance {
String source;
String dest;
String str = "";
int cnt = 1;
int[][] a;
public EditDistance(String source, String dest) {
this.source = source;
this.dest = dest;
//System.out.println(LCS(source.length()-1,dest.length()-1));
a = new int[source.length()+1][dest.length()+1];
LCS_it();
}
public String LCS(int i, int j) {
System.out.println(cnt++);
if(i == -1 || j == -1)
return "";
if(source.charAt(i) == dest.charAt(j))
return LCS(i-1,j-1) + source.charAt(i);
else
{
String first = LCS(i,j-1);
String second = LCS(i-1,j);
return (first.length() > second.length() ? first : second);
}
}
public void LCS_it() {
for (int i = 0; i <= source.length(); i++) {
a[i][0] = 0;
}
for (int i = 0; i <= dest.length(); i++) {
a[0][i] = 0;
}
for (int i = 0; i < source.length(); i++)
for (int j=0; j < dest.length(); j++) {
System.out.println(cnt++);
if(source.charAt(i) == dest.charAt(j))
a[i+1][j+1] = a[i][j] + 1;
else
a[i+1][j+1] = Math.max(a[i+1][j], a[i][j+1]);
}
System.out.println(a[source.length()][dest.length()]);
/* Print array */
for (int i = 0; i <= source.length(); i++) {
for (int j=0; j <= dest.length(); j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
public static void main(String args[]) {
new EditDistance("rappler", "maappse");
}
}