Skip to content

Commit e502f90

Browse files
authored
Create PrintShortestCommonSupersequence.java
1 parent 7f45b08 commit e502f90

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
https://www.techiedelight.com/shortest-common-supersequence-finding-scs/
3+
*/
4+
5+
//Find one Shortest Common Supersequence
6+
// length(X) = m and length(Y) = n
7+
8+
public String findSCS(String X, String Y, int m, int n,int T[][]){
9+
10+
if(m == 0) return Y.substring(0,n);
11+
12+
else if(n == 0) return X.substring(0,m);
13+
14+
else if( X.charAt(n-1) == Y.charAt(m-1) ){
15+
return findSCS(X, Y, m-1, n-1, T) + X.charAt(n-1);
16+
}
17+
else if( T[m-1][n] < T[m][n-1] ){
18+
return findSCS(X, Y, m-1, n, T) + X.charAt(m-1);
19+
}
20+
else{
21+
return findSCS(X, Y, m, n-1, T) + Y.charAt(n-1);
22+
}
23+
}
24+
25+
//Print all Shortest Common Supersequence
26+
// length(X) = m and length(Y) = n
27+
28+
public List<String> findSCS(String X, String Y, int m, int n, int T[][]){
29+
if( m == 0){
30+
List<String> l = new ArrayList<>();
31+
l.add(Y.substring(0,n));
32+
return l;
33+
}
34+
else if( n == 0){
35+
List<String> l = new ArrayList<>();
36+
l.add(X.substring(0,m));
37+
return l;
38+
}
39+
else if( X.charAt(m-1) == Y.charAt(n-1) ){
40+
List<String> scs = findSCS(X, Y, m-1, n-1, T);
41+
List<String> res = new ArrayList<>();
42+
for(String s : scs){
43+
res.add(s+X.charAt(m-1));
44+
}
45+
return res;
46+
}
47+
else if( T[m-1][n] < T[m][n-1] ){
48+
List<String> scs = findSCS(X, Y, m-1, n, T);
49+
List<String> res = new ArrayList<>();
50+
for(String s : scs){
51+
res.add(s+X.charAt(m-1));
52+
}
53+
return res;
54+
}
55+
else if( T[m-1][n] > T[m][n-1] ){
56+
List<String> scs = findSCS(X, Y, m, n-1, T);
57+
List<String> res = new ArrayList<>();
58+
for(String s : scs){
59+
res.add(s+Y.charAt(n-1));
60+
}
61+
return res;
62+
}
63+
else{ //T[m-1][n] == T[m][n-1]
64+
List<String> top = findSCS(X, Y, m-1, n, T);
65+
List<String> left = findSCS(X, Y, m, n-1, T);
66+
List<String> res = new ArrayList<>();
67+
for(String s : top){
68+
res.add(s + X.charAt(m-1));
69+
}
70+
for(String s : left){
71+
res.add(s + Y.charAt(n-1));
72+
}
73+
return res;
74+
}
75+
76+
}
77+
78+
79+
// Function to fill lookup table by finding length of SCS of
80+
// sequences X[0..m-1] and Y[0..n-1]
81+
public static void SCSLength(String X, String Y, int m, int n, int[][] T)
82+
{
83+
// initialize first column of the lookup table
84+
for (int i = 0; i <= m; i++) {
85+
T[i][0] = i;
86+
}
87+
88+
// initialize first row of the lookup table
89+
for (int j = 0; j <= n; j++) {
90+
T[0][j] = j;
91+
}
92+
93+
// fill the lookup table in bottom-up manner
94+
for (int i = 1; i <= m; i++)
95+
{
96+
for (int j = 1; j <= n; j++)
97+
{
98+
// if current character of X and Y matches
99+
if (X.charAt(i - 1) == Y.charAt(j - 1)) {
100+
T[i][j] = T[i - 1][j - 1] + 1;
101+
}
102+
// else if current character of X and Y don't match
103+
else {
104+
T[i][j] = Integer.min(T[i - 1][j] + 1, T[i][j - 1] + 1);
105+
}
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)