-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path实现strStr().cpp
76 lines (68 loc) · 1.09 KB
/
实现strStr().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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <time.h>
#include "linearlist.cpp"
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle)
{
if (needle.size() == 0)
return 0;
vector<int>next = GetNext(needle);
int i = 0;//S的下标
int j = 0;//P的下标
int s_len = haystack.size(), p_len = needle.size();
while (i<s_len&&j<p_len)
{
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j == p_len)
return i - j;
return -1;
}
private:
vector<int> GetNext(string P)
{
int P_len = P.size();
vector<int>next(P_len, -1);
int i = 0, j = -1;
while (i<P_len - 1)
{
if (j == -1 || P[i] == P[j])
{
i++;
j++;
if (P[i] != P[j])
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
return next;
}
};
int main()
{
Solution a;
string S = "aaaaa";
string P = "bba";
cout << a.strStr(S, P) << endl;
system("pause");
}