-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintLCS.java
More file actions
63 lines (54 loc) · 1.53 KB
/
PrintLCS.java
File metadata and controls
63 lines (54 loc) · 1.53 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
public class PrintLCS {
public static void main(String args[]){
int dp[][] = new int[6][7];
String a ="acbcf";
String b = "abcdaf";
for(int i=0; i<6; i++){
for(int j=0; j<7; j++){
if(i==0||j==0){
dp[i][j]=0;
}
}
}
for(int i=1; i<6; i++){
for(int j=1; j<7; j++){
if(a.charAt(i-1)==b.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}
else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
int x=5,y=6;
String s="";
while(x>0 && y>0){
if(a.charAt(x-1) == b.charAt(y-1)){
s =a.charAt(x-1)+ s;
x--;
y--;
}
else{
if(dp[x-1][y] > dp[x][y-1]){
x--;
}
else
y--;
}
}
// for(int i= 0;i<5;i++){
// for(int j=0;j<6;j++){
// if(a.charAt(i) ==b.charAt(j) && s.indexOf(a.charAt(i))==-1){
// s = s+a.charAt(i);
// }
// }
// }
for(int i=0; i<6; i++){
for(int j=0; j<7; j++){
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
System.out.println(s);
}
}