1 | <?php declare(strict_types=1); |
||
10 | class ShortestCommonSuperstring |
||
11 | { |
||
12 | /** |
||
13 | * @var integer Affix length for current iteration |
||
14 | */ |
||
15 | protected $len; |
||
16 | |||
17 | /** |
||
18 | * @var string[] Prefixes of current length |
||
19 | */ |
||
20 | protected $prefixes; |
||
21 | |||
22 | /** |
||
23 | * @var string[] Strings being merged |
||
24 | */ |
||
25 | protected $strings; |
||
26 | |||
27 | /** |
||
28 | * @var string[] Suffixes of current length |
||
29 | */ |
||
30 | protected $suffixes; |
||
31 | |||
32 | /** |
||
33 | * Get the shortest string that contains all given strings |
||
34 | * |
||
35 | * @param string[] $strings |
||
36 | * @return string |
||
37 | */ |
||
38 | 11 | public function getShortest(array $strings): string |
|
57 | |||
58 | /** |
||
59 | * Compare given strings |
||
60 | */ |
||
61 | 9 | protected static function compareStrings(string $a, string $b): int |
|
74 | |||
75 | /** |
||
76 | * Return the list of keys pointing to strings whose prefix is identical to their suffix |
||
77 | * |
||
78 | * @return integer[] |
||
79 | */ |
||
80 | 9 | protected function getIdenticalAffixKeys(): array |
|
93 | |||
94 | /** |
||
95 | * Match and merge a string by key |
||
96 | * |
||
97 | * @param integer $leftKey Left string's key |
||
98 | * @return bool Whether a match was found and strings merged |
||
99 | */ |
||
100 | 9 | protected function mergeString(int $leftKey): bool |
|
115 | |||
116 | /** |
||
117 | * Merge two stored strings together at current affix length |
||
118 | */ |
||
119 | 7 | protected function mergeStringPair(int $leftKey, int $rightKey): void |
|
125 | |||
126 | /** |
||
127 | * Merge all stored strings using current affix length |
||
128 | */ |
||
129 | 9 | protected function mergeStrings(): void |
|
143 | |||
144 | /** |
||
145 | * Match and merge strings from given group |
||
146 | * |
||
147 | * @param integer[] $keys List of keys |
||
148 | * @return void |
||
149 | */ |
||
150 | 9 | protected function mergeStringsGroup(array $keys): void |
|
163 | |||
164 | /** |
||
165 | * Remove empty strings from the list |
||
166 | */ |
||
167 | 11 | protected function removeEmptyStrings(): void |
|
174 | |||
175 | /** |
||
176 | * Remove fully-overlapping strings from the list |
||
177 | */ |
||
178 | 11 | protected function removeFullyOverlappingStrings(): void |
|
202 | |||
203 | /** |
||
204 | * Reset the keys in the string list to remove the gaps |
||
205 | */ |
||
206 | 11 | protected function resetKeys(): void |
|
214 | |||
215 | /** |
||
216 | * Sort the stored strings |
||
217 | */ |
||
218 | 11 | protected function sortStrings(): void |
|
222 | |||
223 | /** |
||
224 | * Capture and stored affixes of current length |
||
225 | * |
||
226 | * Will only store affixes from strings that are longer than current affix length |
||
227 | */ |
||
228 | 9 | protected function storeAffixes(): void |
|
242 | } |