-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintLCS.java
More file actions
62 lines (50 loc) · 1.13 KB
/
Copy pathPrintLCS.java
File metadata and controls
62 lines (50 loc) · 1.13 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
package practiceProject;
public class PrintLCS {
/* Print Longest common subseqence */
static int tp[][] = new int[1001][1001];
static {
for(int i = 0;i<1001;i++) {
for(int j = 0;j<1001;j++) {
tp[i][j] = -1;
}
}
}
public static void main(String[] args) {
String X = "abcdeh";
String Y = "abgdkh";
System.out.println(LCS(X,Y,X.length(),Y.length()));
int i = X.length();
int j = Y.length();
String s = "";
while(i>0 && j>0) {
if(X.charAt(i-1)==Y.charAt(j-1)) {
s += X.charAt(i-1);
i--;
j--;
}else {
if(tp[i-1][j]>tp[i][j-1]) {
i--;
}else {
j--;
}
}
}
StringBuilder sb = new StringBuilder(s);
System.out.println(sb.reverse().toString());
}
public static int LCS(String X, String Y, int xsize, int ysize) {
if(xsize==0||ysize==0) {
return 0;
}
if(tp[xsize][ysize]!=-1) {
return tp[xsize][ysize];
}else {
if(X.charAt(xsize-1)==Y.charAt(ysize-1)) {
tp[xsize][ysize] = 1 + LCS(X, Y, xsize-1, ysize-1);
}else {
tp[xsize][ysize] = Math.max(LCS(X,Y,xsize, ysize-1), LCS(X,Y,xsize-1, ysize));
}
}
return tp[xsize][ysize];
}
}