- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathLongestCommonSubsequence.java
175 lines (148 loc) · 5.82 KB
/
LongestCommonSubsequence.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
packagecom.jwetherell.algorithms.sequence;
importjava.util.HashSet;
importjava.util.Set;
/**
* The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from problems
* of finding common substrings: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences.
* <p>
* @see <a href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem">Longest Common Subsequence Problem (Wikipedia)</a>
* <br>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
@SuppressWarnings("unchecked")
publicclassLongestCommonSubsequence {
privatestaticint[][] lengthMatrix = null;
privatestaticSet<String>[][] sequenceMatrix = null;
privateLongestCommonSubsequence() { }
publicstaticMatrixPairgetLCS(char[] seq1, char[] seq2) {
try {
populateMatrix(seq1, seq2);
for (inti = 0; i < seq1.length; i++) {
for (intj = 0; j < seq2.length; j++) {
lengthMatrix[i + 1][j + 1] = longestCommonSubsequence(i, j, seq1, seq2);
}
}
return (newMatrixPair(lengthMatrix, sequenceMatrix));
} finally {
lengthMatrix = null;
sequenceMatrix = null;
}
}
privatestaticvoidpopulateMatrix(char[] seq1, char[] seq2) {
lengthMatrix = newint[seq1.length + 1][seq2.length + 1];
sequenceMatrix = newHashSet[seq1.length][seq2.length];
}
privatestaticintlongestCommonSubsequence(inti, intj, char[] seq1, char[] seq2) {
charx = seq1[i];
chary = seq2[j];
intresult = 0;
Set<String> set = sequenceMatrix[i][j];
if (set == null)
set = newHashSet<String>();
if (x == y) {
if (i > 0 && j > 0)
set = newHashSet<String>(sequenceMatrix[i - 1][j - 1]);
distribute(x, set);
result = (lengthMatrix[i][j]) + 1;
} else {
inta = lengthMatrix[i][j + 1];
intb = lengthMatrix[i + 1][j];
if (a > b) {
set = newHashSet<String>(sequenceMatrix[i - 1][j]);
result = a;
} elseif (b > a) {
set = newHashSet<String>(sequenceMatrix[i][j - 1]);
result = b;
} elseif (a == b) {
if (i > 0 && j > 0) {
Set<String> list1 = sequenceMatrix[i - 1][j];
Set<String> list2 = sequenceMatrix[i][j - 1];
set.addAll(list1);
set.addAll(list2);
}
result = a; // a==b
}
}
sequenceMatrix[i][j] = set;
returnresult;
}
privatestaticvoiddistribute(charc, Set<String> set) {
if (set.size() == 0) {
set.add(String.valueOf(c));
} else {
Object[] strings = set.toArray();
set.clear();
for (Objectobject : strings) {
Stringstring = (String) object;
Stringseq = String.valueOf(c);
if (!string.contains(seq))
string = string.concat(seq);
set.add(string);
}
}
}
publicstaticclassMatrixPair {
privateint[][] lenMatrix = null;
privateSet<String>[][] seqMatrix = null;
publicMatrixPair(int[][] lengthMatrix, Set<String>[][] sequenceMatrix) {
this.lenMatrix = lengthMatrix;
this.seqMatrix = sequenceMatrix;
}
publicintgetLongestSequenceLength() {
if (lenMatrix == null)
return0;
intlength1 = lenMatrix.length;
intlength2 = lenMatrix[length1 - 1].length;
returnlenMatrix[length1 - 1][length2 - 1];
}
publicSet<String> getLongestSequences() {
if (seqMatrix == null)
return (newHashSet<String>());
intlength1 = seqMatrix.length;
intlength2 = seqMatrix[length1 - 1].length;
returnseqMatrix[length1 - 1][length2 - 1];
}
publicint[][] getLengthMatrix() {
returnlenMatrix;
}
publicSet<String>[][] getSequenceMatrix() {
returnseqMatrix;
}
publicStringgetLengthMatrixString() {
StringBuilderbuilder = newStringBuilder();
if (lenMatrix == null) {
builder.append("Length matrix is NULL.\n");
} else {
for (inti = 0; i < lenMatrix.length; i++) {
intlength = lenMatrix[i].length;
for (intj = 0; j < length; j++) {
intsize = lenMatrix[i][j];
builder.append(size);
if (j < length - 1)
builder.append(",\t");
}
builder.append("\n");
}
}
returnbuilder.toString();
}
publicStringgetSequenceMatrixString() {
StringBuilderbuilder = newStringBuilder();
if (seqMatrix == null) {
builder.append("Sequence matrix is NULL.\n");
} else {
for (inti = 0; i < seqMatrix.length; i++) {
intlength = seqMatrix[i].length;
for (intj = 0; j < length; j++) {
Set<String> set = seqMatrix[i][j];
builder.append(set.toString());
if (j < length - 1)
builder.append(", ");
}
builder.append("\n");
}
}
returnbuilder.toString();
}
}
}