|
59 | 59 |
|
60 | 60 | ## 解法
|
61 | 61 |
|
| 62 | +### 方法一 |
| 63 | + |
| 64 | +<!-- tabs:start --> |
| 65 | + |
| 66 | +```python |
| 67 | +class Solution: |
| 68 | + def beautifulIndices(self, s: str, a: str, b: str, k: int) -> List[int]: |
| 69 | + def build_prefix_function(pattern): |
| 70 | + prefix_function = [0] * len(pattern) |
| 71 | + j = 0 |
| 72 | + for i in range(1, len(pattern)): |
| 73 | + while j > 0 and pattern[i] != pattern[j]: |
| 74 | + j = prefix_function[j - 1] |
| 75 | + if pattern[i] == pattern[j]: |
| 76 | + j += 1 |
| 77 | + prefix_function[i] = j |
| 78 | + return prefix_function |
| 79 | + |
| 80 | + def kmp_search(pattern, text, prefix_function): |
| 81 | + occurrences = [] |
| 82 | + j = 0 |
| 83 | + for i in range(len(text)): |
| 84 | + while j > 0 and text[i] != pattern[j]: |
| 85 | + j = prefix_function[j - 1] |
| 86 | + if text[i] == pattern[j]: |
| 87 | + j += 1 |
| 88 | + if j == len(pattern): |
| 89 | + occurrences.append(i - j + 1) |
| 90 | + j = prefix_function[j - 1] |
| 91 | + return occurrences |
| 92 | + |
| 93 | + prefix_a = build_prefix_function(a) |
| 94 | + prefix_b = build_prefix_function(b) |
| 95 | + |
| 96 | + resa = kmp_search(a, s, prefix_a) |
| 97 | + resb = kmp_search(b, s, prefix_b) |
| 98 | + |
| 99 | + res = [] |
| 100 | + print(resa, resb) |
| 101 | + i = 0 |
| 102 | + j = 0 |
| 103 | + while i < len(resa): |
| 104 | + while j < len(resb): |
| 105 | + if abs(resb[j] - resa[i]) <= k: |
| 106 | + res.append(resa[i]) |
| 107 | + break |
| 108 | + elif j + 1 < len(resb) and abs(resb[j + 1] - resa[i]) < abs( |
| 109 | + resb[j] - resa[i] |
| 110 | + ): |
| 111 | + j += 1 |
| 112 | + else: |
| 113 | + break |
| 114 | + i += 1 |
| 115 | + return res |
| 116 | +``` |
| 117 | + |
| 118 | +```java |
| 119 | +public class Solution { |
| 120 | + public void computeLPS(String pattern, int[] lps) { |
| 121 | + int M = pattern.length(); |
| 122 | + int len = 0; |
| 123 | + |
| 124 | + lps[0] = 0; |
| 125 | + |
| 126 | + int i = 1; |
| 127 | + while (i < M) { |
| 128 | + if (pattern.charAt(i) == pattern.charAt(len)) { |
| 129 | + len++; |
| 130 | + lps[i] = len; |
| 131 | + i++; |
| 132 | + } else { |
| 133 | + if (len != 0) { |
| 134 | + len = lps[len - 1]; |
| 135 | + } else { |
| 136 | + lps[i] = 0; |
| 137 | + i++; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public List<Integer> KMP_codestorywithMIK(String pat, String txt) { |
| 144 | + int N = txt.length(); |
| 145 | + int M = pat.length(); |
| 146 | + List<Integer> result = new ArrayList<>(); |
| 147 | + |
| 148 | + int[] lps = new int[M]; |
| 149 | + computeLPS(pat, lps); |
| 150 | + |
| 151 | + int i = 0; // Index for text |
| 152 | + int j = 0; // Index for pattern |
| 153 | + |
| 154 | + while (i < N) { |
| 155 | + if (pat.charAt(j) == txt.charAt(i)) { |
| 156 | + i++; |
| 157 | + j++; |
| 158 | + } |
| 159 | + |
| 160 | + if (j == M) { |
| 161 | + result.add(i - j); // Pattern found at index i-j+1 (If you have to return 1 Based |
| 162 | + // indexing, that's why added + 1) |
| 163 | + j = lps[j - 1]; |
| 164 | + } else if (i < N && pat.charAt(j) != txt.charAt(i)) { |
| 165 | + if (j != 0) { |
| 166 | + j = lps[j - 1]; |
| 167 | + } else { |
| 168 | + i++; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return result; |
| 174 | + } |
| 175 | + |
| 176 | + private int lowerBound(List<Integer> list, int target) { |
| 177 | + int left = 0, right = list.size() - 1, result = list.size(); |
| 178 | + |
| 179 | + while (left <= right) { |
| 180 | + int mid = left + (right - left) / 2; |
| 181 | + |
| 182 | + if (list.get(mid) >= target) { |
| 183 | + result = mid; |
| 184 | + right = mid - 1; |
| 185 | + } else { |
| 186 | + left = mid + 1; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return result; |
| 191 | + } |
| 192 | + |
| 193 | + public List<Integer> beautifulIndices(String s, String a, String b, int k) { |
| 194 | + int n = s.length(); |
| 195 | + |
| 196 | + List<Integer> i_indices = KMP_codestorywithMIK(a, s); |
| 197 | + List<Integer> j_indices = KMP_codestorywithMIK(b, s); |
| 198 | + |
| 199 | + List<Integer> result = new ArrayList<>(); |
| 200 | + |
| 201 | + for (int i : i_indices) { |
| 202 | + |
| 203 | + int left_limit = Math.max(0, i - k); // To avoid out of bound -> I used max(0, i-k) |
| 204 | + int right_limit |
| 205 | + = Math.min(n - 1, i + k); // To avoid out of bound -> I used min(n-1, i+k) |
| 206 | + |
| 207 | + int lowerBoundIndex = lowerBound(j_indices, left_limit); |
| 208 | + |
| 209 | + if (lowerBoundIndex < j_indices.size() |
| 210 | + && j_indices.get(lowerBoundIndex) <= right_limit) { |
| 211 | + result.add(i); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return result; |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +```cpp |
| 221 | +class Solution { |
| 222 | +public: |
| 223 | + vector<int> beautifulIndices(string s, string patternA, string patternB, int k) { |
| 224 | + vector<int> beautifulIndicesA = kmpSearch(s, patternA); |
| 225 | + vector<int> beautifulIndicesB = kmpSearch(s, patternB); |
| 226 | + |
| 227 | + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); |
| 228 | + |
| 229 | + vector<int> result; |
| 230 | + for (int indexA : beautifulIndicesA) { |
| 231 | + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); |
| 232 | + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); |
| 233 | + |
| 234 | + left = (left >= 0) ? left : -(left + 1); |
| 235 | + right = (right >= 0) ? right : -(right + 1); |
| 236 | + |
| 237 | + for (int indexB = left; indexB < right; indexB++) { |
| 238 | + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { |
| 239 | + result.push_back(indexA); |
| 240 | + break; |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + return result; |
| 246 | + } |
| 247 | + |
| 248 | +private: |
| 249 | + vector<int> kmpSearch(string text, string pattern) { |
| 250 | + vector<int> indices; |
| 251 | + vector<int> pi = computePrefixFunction(pattern); |
| 252 | + |
| 253 | + int q = 0; |
| 254 | + for (int i = 0; i < text.length(); i++) { |
| 255 | + while (q > 0 && pattern[q] != text[i]) { |
| 256 | + q = pi[q - 1]; |
| 257 | + } |
| 258 | + if (pattern[q] == text[i]) { |
| 259 | + q++; |
| 260 | + } |
| 261 | + if (q == pattern.length()) { |
| 262 | + indices.push_back(i - q + 1); |
| 263 | + q = pi[q - 1]; |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + return indices; |
| 268 | + } |
| 269 | + |
| 270 | + vector<int> computePrefixFunction(string pattern) { |
| 271 | + int m = pattern.length(); |
| 272 | + vector<int> pi(m, 0); |
| 273 | + int k = 0; |
| 274 | + for (int q = 1; q < m; q++) { |
| 275 | + while (k > 0 && pattern[k] != pattern[q]) { |
| 276 | + k = pi[k - 1]; |
| 277 | + } |
| 278 | + if (pattern[k] == pattern[q]) { |
| 279 | + k++; |
| 280 | + } |
| 281 | + pi[q] = k; |
| 282 | + } |
| 283 | + return pi; |
| 284 | + } |
| 285 | +}; |
| 286 | +``` |
| 287 | +
|
| 288 | +```go |
| 289 | +func beautifulIndices(s string, a string, b string, k int) []int { |
| 290 | +
|
| 291 | + s_len := len(s) |
| 292 | + a_len := len(a) |
| 293 | + b_len := len(b) |
| 294 | +
|
| 295 | + final := make([]int, 0) |
| 296 | + lps_a := make([]int, a_len) |
| 297 | + lps_b := make([]int, b_len) |
| 298 | + a_index := make([]int, 0) |
| 299 | + b_index := make([]int, 0) |
| 300 | +
|
| 301 | + var pat func(lps []int, s_l int, pattern string) |
| 302 | +
|
| 303 | + pat = func(lps []int, s_l int, pattern string) { |
| 304 | +
|
| 305 | + l := 0 |
| 306 | + lps[0] = 0 |
| 307 | + i := 1 |
| 308 | +
|
| 309 | + for i < s_l { |
| 310 | + if pattern[i] == pattern[l] { |
| 311 | + l++ |
| 312 | + lps[i] = l |
| 313 | + i++ |
| 314 | + } else { |
| 315 | + if l != 0 { |
| 316 | + l = lps[l-1] |
| 317 | + } else { |
| 318 | + lps[i] = l |
| 319 | + i++ |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | +
|
| 325 | + pat(lps_a, a_len, a) |
| 326 | + pat(lps_b, b_len, b) |
| 327 | +
|
| 328 | + var kmp func(pat string, pat_l int, lps []int, index *[]int) |
| 329 | +
|
| 330 | + kmp = func(pat string, pat_l int, lps []int, index *[]int) { |
| 331 | + i := 0 |
| 332 | + j := 0 |
| 333 | + for s_len-i >= pat_l-j { |
| 334 | + if s[i] == pat[j] { |
| 335 | + i++ |
| 336 | + j++ |
| 337 | + } |
| 338 | + if j == pat_l { |
| 339 | + *index = append(*index, i-pat_l) |
| 340 | + j = lps[j-1] |
| 341 | + } else if s[i] != pat[j] { |
| 342 | + if j != 0 { |
| 343 | + j = lps[j-1] |
| 344 | + } else { |
| 345 | + i++ |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + } |
| 350 | +
|
| 351 | + kmp(a, a_len, lps_a, &a_index) |
| 352 | + kmp(b, b_len, lps_b, &b_index) |
| 353 | +
|
| 354 | + // fmt.Println(a_index, b_index) |
| 355 | +
|
| 356 | + i := 0 |
| 357 | + j := 0 |
| 358 | +
|
| 359 | + for i < len(a_index) && j < len(b_index) { |
| 360 | + if a_index[i]+k >= b_index[j] && a_index[i]-k <= b_index[j] { |
| 361 | + final = append(final, a_index[i]) |
| 362 | + i++ |
| 363 | + } else if a_index[i]-k > b_index[j] { |
| 364 | + j++ |
| 365 | + } else { |
| 366 | + i++ |
| 367 | + } |
| 368 | + } |
| 369 | +
|
| 370 | + return final |
| 371 | +} |
| 372 | +``` |
| 373 | + |
| 374 | +<!-- tabs:end --> |
| 375 | + |
62 | 376 | <!-- end -->
|
0 commit comments