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 |
|
57 | |||
58 | /** |
||
59 | * Compare given strings |
||
60 | */ |
||
61 | 10 | protected static function compareStrings(string $a, string $b): int |
|
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 blank this string's prefix from the array to avoid matches |
||
84 | 7 | $prefix = $this->prefixes[$leftKey]; |
|
85 | 7 | $this->prefixes[$leftKey] = ''; |
|
86 | |||
87 | // Search for a prefix that matches this string's suffix before restoring its prefix |
||
88 | 7 | $rightKey = array_search($this->suffixes[$leftKey], $this->prefixes, true); |
|
89 | 7 | $this->prefixes[$leftKey] = $prefix; |
|
90 | |||
91 | 7 | if ($rightKey === false) |
|
92 | { |
||
93 | 7 | return false; |
|
94 | } |
||
95 | |||
96 | 7 | $this->mergeStringPair($leftKey, $rightKey); |
|
97 | |||
98 | 7 | return true; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Merge two stored strings together at current affix length |
||
103 | */ |
||
104 | 7 | protected function mergeStringPair(int $leftKey, int $rightKey): void |
|
105 | { |
||
106 | 7 | $this->strings[$leftKey] .= substr($this->strings[$rightKey], $this->len); |
|
107 | 7 | $this->suffixes[$leftKey] = $this->suffixes[$rightKey]; |
|
108 | 7 | unset($this->prefixes[$rightKey], $this->strings[$rightKey], $this->suffixes[$rightKey]); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Merge all stored strings using current affix length |
||
113 | */ |
||
114 | 10 | protected function mergeStrings(): void |
|
115 | { |
||
116 | 10 | $this->storeAffixes(); |
|
117 | |||
118 | // Merge strings whose prefix is identical to their suffix |
||
119 | 10 | $keys = array_keys(array_intersect_assoc($this->prefixes, $this->suffixes)); |
|
120 | 10 | $this->mergeStringsGroup($keys); |
|
121 | |||
122 | // Merge strings that have a suffix that matches a prefix |
||
123 | 10 | $keys = array_keys(array_intersect($this->suffixes, $this->prefixes)); |
|
124 | 10 | $this->mergeStringsGroup($keys); |
|
125 | |||
126 | 10 | $this->resetKeys(); |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Match and merge strings from given group |
||
131 | * |
||
132 | * @param integer[] $keys List of keys |
||
133 | * @return void |
||
134 | */ |
||
135 | 10 | protected function mergeStringsGroup(array $keys): void |
|
136 | { |
||
137 | 10 | foreach ($keys as $leftKey) |
|
138 | { |
||
139 | 7 | if (isset($this->strings[$leftKey])) |
|
140 | { |
||
141 | 7 | while ($this->mergeString($leftKey)) |
|
142 | { |
||
143 | // Keep going |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Remove empty strings from the list |
||
151 | */ |
||
152 | 12 | protected function removeEmptyStrings(): void |
|
153 | { |
||
154 | 12 | if (end($this->strings) === '') |
|
155 | { |
||
156 | 1 | array_pop($this->strings); |
|
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Remove fully-overlapping strings from the list |
||
162 | */ |
||
163 | 12 | protected function removeFullyOverlappingStrings(): void |
|
164 | { |
||
165 | // Copy the list of strings ordered by ascending length and create a master string by |
||
166 | // concatenating them all |
||
167 | 12 | $strings = array_reverse($this->strings, true); |
|
168 | 12 | $all = implode('', $strings); |
|
169 | 12 | $pos = 0; |
|
170 | 12 | foreach ($strings as $i => $str) |
|
171 | { |
||
172 | // Test whether current string potentially appears in any subsequent, bigger strings |
||
173 | 10 | $pos += strlen($str); |
|
174 | 10 | if (strpos($all, $str, $pos) === false) |
|
175 | { |
||
176 | 10 | continue; |
|
177 | } |
||
178 | |||
179 | // Iterate over strings from the longest to the one before current string |
||
180 | 2 | $j = -1; |
|
181 | 2 | while (++$j < $i) |
|
182 | { |
||
183 | 2 | if (strpos($strings[$j], $str) !== false) |
|
184 | { |
||
185 | 2 | unset($this->strings[$i]); |
|
186 | 2 | break; |
|
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | 12 | $this->resetKeys(); |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Reset the keys in the string list to remove the gaps |
||
196 | */ |
||
197 | 12 | protected function resetKeys(): void |
|
205 | |||
206 | /** |
||
207 | * Sort the stored strings |
||
208 | */ |
||
209 | 12 | protected function sortStrings(): void |
|
213 | |||
214 | /** |
||
215 | * Capture and stored affixes of current length |
||
216 | * |
||
217 | * Will only store affixes from strings that are longer than current affix length |
||
218 | */ |
||
219 | 10 | protected function storeAffixes(): void |
|
233 | } |