Skip to content

Commit fca1c44

Browse files
committed
28
1 parent e690816 commit fca1c44

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

leetcode/28/0.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"aabaaabaaac"
2+
"aabaaac"

leetcode/28/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution-1.cpp"
8+
using namespace std;
9+
10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
13+
int main()
14+
{
15+
LeetCodeInput li("0.in");
16+
17+
auto l0 = li.get<string>(0);
18+
auto l1 = li.get<string>(1);
19+
20+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
21+
Solution solution;
22+
auto output = solution.strStr(l0, l1);
23+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
24+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
25+
26+
cout << output << endl;
27+
28+
// for (int i: output){
29+
// cout << i << ' ';
30+
// }
31+
32+
// for (auto i: output){
33+
// for (int j: i){
34+
// cout << j << ' ';
35+
// }
36+
// cout << '\n';
37+
// }
38+
39+
return 0;
40+
}

leetcode/28/solution-1.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
3+
time: O(m+n)
4+
space: O(n)
5+
6+
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Find the Index of the First Occurrence in a String.
7+
Memory Usage: 6.2 MB, less than 60.31% of C++ online submissions for Find the Index of the First Occurrence in a String.
8+
kmp
9+
*/
10+
11+
#include <bits/stdc++.h>
12+
13+
using namespace std;
14+
15+
#define all(x) begin(x), end(x)
16+
typedef vector<int> vi;
17+
typedef vector<vector<int>> vvi;
18+
19+
class Solution {
20+
public:
21+
int strStr(string text, string pattern) {
22+
int m = size(text);
23+
int n = size(pattern);
24+
/// fallback array
25+
int fb[n];
26+
fb[0] = 0;
27+
for (int i=1, j=0; i<n;){
28+
if (pattern[i] == pattern[j]){
29+
++j;
30+
fb[i] = j;
31+
++i;
32+
} else {
33+
if (j>0){
34+
j = fb[j-1];
35+
} else {
36+
fb[i] = 0;
37+
++i;
38+
}
39+
}
40+
}
41+
42+
for (int i=0, j=0; i<=m;){
43+
if (text[i] == pattern[j]){
44+
++i;
45+
++j;
46+
if (j==n){
47+
return i-j;
48+
}
49+
} else {
50+
if (j>0){
51+
j = fb[j-1];
52+
} else {
53+
++i;
54+
}
55+
}
56+
}
57+
58+
return -1;
59+
}
60+
};
61+
62+
const static auto initialize = [] {
63+
std::ios::sync_with_stdio(false);
64+
std::cin.tie(nullptr);
65+
std::cout.tie(nullptr);
66+
return nullptr;
67+
}();

leetcode/28/solution.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Find the Index of the First Occurrence in a String.
7+
Memory Usage: 6.2 MB, less than 60.31% of C++ online submissions for Find the Index of the First Occurrence in a String.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
int strStr(string haystack, string needle) {
21+
return haystack.find(needle);
22+
}
23+
};
24+
25+
const static auto initialize = [] {
26+
std::ios::sync_with_stdio(false);
27+
std::cin.tie(nullptr);
28+
std::cout.tie(nullptr);
29+
return nullptr;
30+
}();

0 commit comments

Comments
 (0)