-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestCommonSuperSequence.java
More file actions
130 lines (108 loc) · 3.52 KB
/
ShortestCommonSuperSequence.java
File metadata and controls
130 lines (108 loc) · 3.52 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
package Dynamic_Programming;
import java.util.*;
public class ShortestCommonSuperSequence {
// MEMOIZATION
public static int SCS_memo(String s1, String s2){
// base case
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n+1][m+1];
for (int i=0; i<dp.length; i++){
Arrays.fill(dp[i], -1);
}
int lcs = lcs(n, m, s1, s2, dp);
// scs = (n-lcs) + (m-lcs) + lcs
return (n+m) - lcs;
}
public static int lcs(int ind1, int ind2, String s1, String s2, int[][] dp){
// base case
if (ind1 == 0 || ind2 == 0) return 0;
if (dp[ind1][ind2] != -1) return dp[ind1][ind2];
if (s1.charAt(ind1-1) == s2.charAt(ind2-1)) {
return dp[ind1][ind2] = 1 + lcs(ind1-1, ind2-1, s1, s2, dp);
}else {
int len1 = lcs(ind1-1, ind2, s1, s2, dp);
int len2 = lcs(ind1, ind2-1, s1, s2, dp);
return dp[ind1][ind2] = Math.max(len1, len2);
}
}
// TABULATION
public static int SCS_Tab(String s1, String s2){
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n+1][m+1];
for (int i=1; i<dp.length; i++){
for (int j=1; j<dp[0].length; j++){
// matches
if (s1.charAt(i-1) == s2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + 1;
}else{
int len1 = dp[i-1][j];
int len2 = dp[i][j-1];
dp[i][j] = Math.max(len1, len2);
}
}
}
int lcs = dp[n][m];
int scs = (n-lcs) + (m-lcs) + lcs;
return scs;
}
public static void printSCS(String s1, String s2){
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n+1][m+1];
for (int i=1; i<dp.length; i++){
for (int j=1; j<dp[0].length; j++){
// matches
if (s1.charAt(i-1) == s2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + 1;
}else{
int len1 = dp[i-1][j];
int len2 = dp[i][j-1];
dp[i][j] = Math.max(len1, len2);
}
}
}
StringBuilder scs = new StringBuilder();
int i = n;
int j = m;
while (i > 0 && j > 0){
if (s1.charAt(i-1) == s2.charAt(j-1)){
scs.append(s1.charAt(i-1));
// don't include both
i--;
j--;
}else if (dp[i-1][j] > dp[i][j-1]) {
// up
scs.append(s1.charAt(i-1));
i--;
}else {
// left
scs.append(s2.charAt(j-1));
j--;
}
}
// remaining
while (i > 0){
scs.append(s1.charAt(i-1));
i--;
}
while (j > 0) {
scs.append(s2.charAt(j-1));
j--;
}
String ans = scs.reverse().toString();
System.out.println(ans);
}
public static void main(String[] args) {
String s1 = "brute";
String s2 = "groot";
// ans = 8
// Memoize
System.out.println(SCS_memo(s1, s2));
// Tabulation
System.out.println(SCS_Tab(s1, s2));
// Print SCS
printSCS(s1, s2);
}
}