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 | 12 | public function getShortest(array $strings): string |
|
39 | { |
||
40 | 12 | $this->strings = array_unique($strings); |
|
41 | 12 | $this->sortStrings(); |
|
42 | 12 | $this->removeEmptyStrings(); |
|
43 | 12 | $this->removeFullyOverlappingStrings(); |
|
44 | 12 | if (isset($this->strings[1])) |
|
45 | { |
||
46 | // Start with the longest partial match possible, which is equal to the length of the |
||
47 | // second longest string minus 1 |
||
48 | 10 | $this->len = strlen($this->strings[1]); |
|
49 | 10 | while (--$this->len > 0) |
|
50 | { |
||
51 | 10 | $this->mergeStrings(); |
|
52 | } |
||
53 | } |
||
54 | |||
55 | 12 | return implode('', $this->strings); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Compare given strings |
||
60 | */ |
||
61 | 10 | protected static function compareStrings(string $a, string $b): int |
|
62 | { |
||
63 | 10 | $aLen = strlen($a); |
|
64 | 10 | $bLen = strlen($b); |
|
65 | 10 | if ($aLen !== $bLen) |
|
66 | { |
||
67 | // Longest first |
||
68 | 4 | return $bLen - $aLen; |
|
69 | } |
||
70 | |||
71 | // Lexical order |
||
72 | 10 | return ($a > $b) ? 1 : -1; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Match and merge a string by key |
||
77 | * |
||
78 | * @param integer $leftKey Left string's key |
||
79 | * @return bool Whether a match was found and strings merged |
||
80 | */ |
||
81 | 7 | protected function mergeString(int $leftKey): bool |
|
82 | { |
||
83 | // Temporarily remove this string's prefix from the array to avoid matches |
||
84 | 7 | if (isset($this->prefixes[$leftKey])) |
|
85 | { |
||
86 | 6 | $prefix = $this->prefixes[$leftKey]; |
|
87 | 6 | unset($this->prefixes[$leftKey]); |
|
88 | } |
||
89 | |||
90 | // Search for a prefix that matches this string's suffix before restoring its prefix |
||
91 | 7 | $rightKey = array_search($this->suffixes[$leftKey], $this->prefixes, true); |
|
92 | 7 | if (isset($prefix)) |
|
93 | { |
||
94 | 6 | $this->prefixes[$leftKey] = $prefix; |
|
95 | } |
||
96 | |||
97 | 7 | if ($rightKey === false) |
|
98 | { |
||
99 | 2 | return false; |
|
100 | } |
||
101 | |||
102 | 7 | $this->mergeStringPair($leftKey, $rightKey); |
|
103 | |||
104 | 7 | return true; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Merge two stored strings together at current affix length |
||
109 | */ |
||
110 | 7 | protected function mergeStringPair(int $leftKey, int $rightKey): void |
|
111 | { |
||
112 | 7 | $this->strings[$leftKey] .= substr($this->strings[$rightKey], $this->len); |
|
113 | 7 | if (isset($this->suffixes[$rightKey])) |
|
114 | { |
||
115 | 1 | $this->suffixes[$leftKey] = $this->suffixes[$rightKey]; |
|
116 | } |
||
117 | else |
||
118 | { |
||
119 | 6 | unset($this->suffixes[$leftKey]); |
|
120 | } |
||
121 | 7 | unset($this->prefixes[$rightKey], $this->strings[$rightKey], $this->suffixes[$rightKey]); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Merge all stored strings using current affix length |
||
126 | */ |
||
127 | 10 | protected function mergeStrings(): void |
|
141 | |||
142 | /** |
||
143 | * Match and merge strings from given group |
||
144 | * |
||
145 | * @param integer[] $keys List of keys |
||
146 | * @return void |
||
147 | */ |
||
148 | 10 | protected function mergeStringsGroup(array $keys): void |
|
149 | { |
||
150 | 10 | foreach ($keys as $leftKey) |
|
151 | { |
||
152 | 7 | while (isset($this->suffixes[$leftKey]) && $this->mergeString($leftKey)) |
|
153 | { |
||
154 | // Keep going as long as the left key has a suffix that matches a prefix |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Remove empty strings from the list |
||
161 | */ |
||
162 | 12 | protected function removeEmptyStrings(): void |
|
169 | |||
170 | /** |
||
171 | * Remove fully-overlapping strings from the list |
||
172 | */ |
||
173 | 12 | protected function removeFullyOverlappingStrings(): void |
|
203 | |||
204 | /** |
||
205 | * Reset the keys in the string list to remove the gaps |
||
206 | */ |
||
207 | 12 | protected function resetKeys(): void |
|
215 | |||
216 | /** |
||
217 | * Sort the stored strings |
||
218 | */ |
||
219 | 12 | protected function sortStrings(): void |
|
223 | |||
224 | /** |
||
225 | * Capture and store matching affixes of current length |
||
226 | * |
||
227 | * Will only store affixes from strings that are longer than current affix length and that have |
||
228 | * a match on the other end of a string |
||
229 | */ |
||
230 | 10 | protected function storeMatchingAffixes(): void |
|
248 | } |