diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md index ed34249d3dfc7..bdeaf0f21d46f 100644 --- a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md @@ -57,4 +57,72 @@ ## 解法 +```cpp +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +}; +``` + diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md index 5d9c6473ac8ea..e636b285164f9 100644 --- a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md @@ -55,4 +55,72 @@ Thus we return [0] as the result. ## Solutions +```cpp +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +}; +``` + diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp new file mode 100644 index 0000000000000..e0e0386675cae --- /dev/null +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp @@ -0,0 +1,65 @@ +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +};