-
Notifications
You must be signed in to change notification settings - Fork 34
/
PrintShortestCommonSupersequence.java
108 lines (96 loc) · 2.8 KB
/
PrintShortestCommonSupersequence.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
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
/*
https://www.techiedelight.com/shortest-common-supersequence-finding-scs/
*/
//Find one Shortest Common Supersequence
// length(X) = m and length(Y) = n
public String findSCS(String X, String Y, int m, int n,int T[][]){
if(m == 0) return Y.substring(0,n);
else if(n == 0) return X.substring(0,m);
else if( X.charAt(n-1) == Y.charAt(m-1) ){
return findSCS(X, Y, m-1, n-1, T) + X.charAt(n-1);
}
else if( T[m-1][n] < T[m][n-1] ){
return findSCS(X, Y, m-1, n, T) + X.charAt(m-1);
}
else{
return findSCS(X, Y, m, n-1, T) + Y.charAt(n-1);
}
}
//Print all Shortest Common Supersequence
// length(X) = m and length(Y) = n
public List<String> findSCS(String X, String Y, int m, int n, int T[][]){
if( m == 0){
List<String> l = new ArrayList<>();
l.add(Y.substring(0,n));
return l;
}
else if( n == 0){
List<String> l = new ArrayList<>();
l.add(X.substring(0,m));
return l;
}
else if( X.charAt(m-1) == Y.charAt(n-1) ){
List<String> scs = findSCS(X, Y, m-1, n-1, T);
List<String> res = new ArrayList<>();
for(String s : scs){
res.add(s+X.charAt(m-1));
}
return res;
}
else if( T[m-1][n] < T[m][n-1] ){
List<String> scs = findSCS(X, Y, m-1, n, T);
List<String> res = new ArrayList<>();
for(String s : scs){
res.add(s+X.charAt(m-1));
}
return res;
}
else if( T[m-1][n] > T[m][n-1] ){
List<String> scs = findSCS(X, Y, m, n-1, T);
List<String> res = new ArrayList<>();
for(String s : scs){
res.add(s+Y.charAt(n-1));
}
return res;
}
else{ //T[m-1][n] == T[m][n-1]
List<String> top = findSCS(X, Y, m-1, n, T);
List<String> left = findSCS(X, Y, m, n-1, T);
List<String> res = new ArrayList<>();
for(String s : top){
res.add(s + X.charAt(m-1));
}
for(String s : left){
res.add(s + Y.charAt(n-1));
}
return res;
}
}
// Function to fill lookup table by finding length of SCS of
// sequences X[0..m-1] and Y[0..n-1]
public static void SCSLength(String X, String Y, int m, int n, int[][] T)
{
// initialize first column of the lookup table
for (int i = 0; i <= m; i++) {
T[i][0] = i;
}
// initialize first row of the lookup table
for (int j = 0; j <= n; j++) {
T[0][j] = j;
}
// fill the lookup table in bottom-up manner
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
// if current character of X and Y matches
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
T[i][j] = T[i - 1][j - 1] + 1;
}
// else if current character of X and Y don't match
else {
T[i][j] = Integer.min(T[i - 1][j] + 1, T[i][j - 1] + 1);
}
}
}
}