add: 230303
This commit is contained in:
parent
f76f1f9566
commit
755c0c2800
|
|
@ -0,0 +1,33 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 28. Find the Index of the First Occurrence in a String
|
||||
*
|
||||
* Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int strStr(const std::string& s, const std::string& t) {
|
||||
const int m = s.length(), n = t.length();
|
||||
std::vector<int> p(n);
|
||||
for (int i = 1; i < n; ++i) {
|
||||
int j = p[i - 1];
|
||||
for (; j > 0 && t[i] != t[j]; j = p[j - 1]);
|
||||
p[i] = j + (t[i] == t[j]);
|
||||
}
|
||||
for (int i = 0, j = 0; i < m; ++i) {
|
||||
for (; j > 0 && s[i] != t[j]; j = p[j - 1]);
|
||||
if (s[i] == t[j] && ++j >= n)
|
||||
return i - j + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::strStr("busasasasasapsadpp", "sasasasapsad");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2303)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2303 230302.cpp)
|
||||
ADD_EXECUTABLE(2303 230303.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue