-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28.实现-str-str.cpp
55 lines (51 loc) · 1.34 KB
/
28.实现-str-str.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode.cn id=28 lang=cpp
*
* [28] 实现 strStr()
*/
// @lc code=start
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
int M = needle.size();
int N = haystack.size();
if(N==0 && M==0)return 0;
if(N==0 && M!=0)return -1;
if(N!=0 && M==0)return 0;
vector<vector<int> > dp(M, vector<int>(256, 0));
dp[0][needle[0]] = 1;
int X = 0;
for(int j = 1; j < M; ++j)
{
for(int c = 0; c < 256; ++c)
{
dp[j][c] = dp[X][c];
}
dp[j][needle[j]] = j + 1;
X = dp[X][needle[j]];
//这始终记录的是与j具有最长的相同前缀
}
int j = 0;
for(int i=0;i<N;++i)
{
j = dp[j][haystack[i]];
if(j == M) return i - M + 1;
}
return -1;
}
// int strStr(string haystack, string needle) {
// if(needle=="")return 0;
// if(haystack.length()<needle.length())return -1;
// for(int i=0;i<haystack.length()-needle.length()+1;++i){
// if(haystack.substr(i,needle.length())==needle){
// return i;
// }
// }
// return -1;
// }
};
// @lc code=end