Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bad77f

Browse files
feat: add cpp solution to lc problem: No.3008 (doocs#2238)
1 parent e79e15a commit 0bad77f

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

‎solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,72 @@
5757

5858
## 解法
5959

60+
```cpp
61+
class Solution {
62+
public:
63+
vector<int> beautifulIndices(string s, string patternA, string patternB, int k) {
64+
vector<int> beautifulIndicesA = kmpSearch(s, patternA);
65+
vector<int> beautifulIndicesB = kmpSearch(s, patternB);
66+
67+
sort(beautifulIndicesB.begin(), beautifulIndicesB.end());
68+
69+
vector<int> result;
70+
for (int indexA : beautifulIndicesA) {
71+
int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin();
72+
int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin();
73+
74+
left = (left >= 0) ? left : -(left + 1);
75+
right = (right >= 0) ? right : -(right + 1);
76+
77+
for (int indexB = left; indexB < right; indexB++) {
78+
if (abs(beautifulIndicesB[indexB] - indexA) <= k) {
79+
result.push_back(indexA);
80+
break;
81+
}
82+
}
83+
}
84+
85+
return result;
86+
}
87+
88+
private:
89+
vector<int> kmpSearch(string text, string pattern) {
90+
vector<int> indices;
91+
vector<int> pi = computePrefixFunction(pattern);
92+
93+
int q = 0;
94+
for (int i = 0; i < text.length(); i++) {
95+
while (q > 0 && pattern[q] != text[i]) {
96+
q = pi[q - 1];
97+
}
98+
if (pattern[q] == text[i]) {
99+
q++;
100+
}
101+
if (q == pattern.length()) {
102+
indices.push_back(i - q + 1);
103+
q = pi[q - 1];
104+
}
105+
}
106+
107+
return indices;
108+
}
109+
110+
vector<int> computePrefixFunction(string pattern) {
111+
int m = pattern.length();
112+
vector<int> pi(m, 0);
113+
int k = 0;
114+
for (int q = 1; q < m; q++) {
115+
while (k > 0 && pattern[k] != pattern[q]) {
116+
k = pi[k - 1];
117+
}
118+
if (pattern[k] == pattern[q]) {
119+
k++;
120+
}
121+
pi[q] = k;
122+
}
123+
return pi;
124+
}
125+
};
126+
```
127+
60128
<!-- end -->

‎solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,72 @@ Thus we return [0] as the result.
5555

5656
## Solutions
5757

58+
```cpp
59+
class Solution {
60+
public:
61+
vector<int> beautifulIndices(string s, string patternA, string patternB, int k) {
62+
vector<int> beautifulIndicesA = kmpSearch(s, patternA);
63+
vector<int> beautifulIndicesB = kmpSearch(s, patternB);
64+
65+
sort(beautifulIndicesB.begin(), beautifulIndicesB.end());
66+
67+
vector<int> result;
68+
for (int indexA : beautifulIndicesA) {
69+
int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin();
70+
int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin();
71+
72+
left = (left >= 0) ? left : -(left + 1);
73+
right = (right >= 0) ? right : -(right + 1);
74+
75+
for (int indexB = left; indexB < right; indexB++) {
76+
if (abs(beautifulIndicesB[indexB] - indexA) <= k) {
77+
result.push_back(indexA);
78+
break;
79+
}
80+
}
81+
}
82+
83+
return result;
84+
}
85+
86+
private:
87+
vector<int> kmpSearch(string text, string pattern) {
88+
vector<int> indices;
89+
vector<int> pi = computePrefixFunction(pattern);
90+
91+
int q = 0;
92+
for (int i = 0; i < text.length(); i++) {
93+
while (q > 0 && pattern[q] != text[i]) {
94+
q = pi[q - 1];
95+
}
96+
if (pattern[q] == text[i]) {
97+
q++;
98+
}
99+
if (q == pattern.length()) {
100+
indices.push_back(i - q + 1);
101+
q = pi[q - 1];
102+
}
103+
}
104+
105+
return indices;
106+
}
107+
108+
vector<int> computePrefixFunction(string pattern) {
109+
int m = pattern.length();
110+
vector<int> pi(m, 0);
111+
int k = 0;
112+
for (int q = 1; q < m; q++) {
113+
while (k > 0 && pattern[k] != pattern[q]) {
114+
k = pi[k - 1];
115+
}
116+
if (pattern[k] == pattern[q]) {
117+
k++;
118+
}
119+
pi[q] = k;
120+
}
121+
return pi;
122+
}
123+
};
124+
```
125+
58126
<!-- end -->
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
vector<int> beautifulIndices(string s, string patternA, string patternB, int k) {
4+
vector<int> beautifulIndicesA = kmpSearch(s, patternA);
5+
vector<int> beautifulIndicesB = kmpSearch(s, patternB);
6+
7+
sort(beautifulIndicesB.begin(), beautifulIndicesB.end());
8+
9+
vector<int> result;
10+
for (int indexA : beautifulIndicesA) {
11+
int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin();
12+
int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin();
13+
14+
left = (left >= 0) ? left : -(left + 1);
15+
right = (right >= 0) ? right : -(right + 1);
16+
17+
for (int indexB = left; indexB < right; indexB++) {
18+
if (abs(beautifulIndicesB[indexB] - indexA) <= k) {
19+
result.push_back(indexA);
20+
break;
21+
}
22+
}
23+
}
24+
25+
return result;
26+
}
27+
28+
private:
29+
vector<int> kmpSearch(string text, string pattern) {
30+
vector<int> indices;
31+
vector<int> pi = computePrefixFunction(pattern);
32+
33+
int q = 0;
34+
for (int i = 0; i < text.length(); i++) {
35+
while (q > 0 && pattern[q] != text[i]) {
36+
q = pi[q - 1];
37+
}
38+
if (pattern[q] == text[i]) {
39+
q++;
40+
}
41+
if (q == pattern.length()) {
42+
indices.push_back(i - q + 1);
43+
q = pi[q - 1];
44+
}
45+
}
46+
47+
return indices;
48+
}
49+
50+
vector<int> computePrefixFunction(string pattern) {
51+
int m = pattern.length();
52+
vector<int> pi(m, 0);
53+
int k = 0;
54+
for (int q = 1; q < m; q++) {
55+
while (k > 0 && pattern[k] != pattern[q]) {
56+
k = pi[k - 1];
57+
}
58+
if (pattern[k] == pattern[q]) {
59+
k++;
60+
}
61+
pi[q] = k;
62+
}
63+
return pi;
64+
}
65+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /