-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution028.cpp
More file actions
93 lines (86 loc) · 1.89 KB
/
solution028.cpp
File metadata and controls
93 lines (86 loc) · 1.89 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Implement strStr()
* KMP algorithm, build next table.
* To calculate next value, we need to find a string which has property that it is longest prefix string as well as suffix string.
*
* cpselvis ([email protected])
* August 24th, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
int j = 0;
vector<int> next(needle.size());
if ((haystack.size() == 0 && needle.size() == 0) || needle.size() == 0)
{
return 0;
}
buildNextTable(needle, next);
for (int i = 0; i < haystack.size(); i ++)
{
while (j > 0 && haystack[i] != needle[j])
{
j = next[j - 1];
}
if (haystack[i] == needle[j])
{
j ++;
}
else
{
j = 0;
}
if (j == needle.size())
{
return i - j + 1;
}
}
return -1;
}
/**
* @brief Build next table.
* Next table is longest prefix and suffix length of each index.
*/
void buildNextTable(string needle, vector<int> &next)
{
next[0] = 0;
for (int i = 1; i < needle.size(); i ++)
{
int tmp = next[i - 1];
while (tmp && needle[tmp] != needle[i])
{
tmp = next[tmp - 1];
}
if (needle[tmp] == needle[i])
{
next[i] = tmp + 1;
}
else
{
next[i] = 0;
}
}
}
};
int main(int argc, char **argv)
{
Solution s;
// Test build next table function.
// string str = "agctagcagctagctg";
//vector<int> vec(17);
//s.buildNextTable(str, vec);
//for (auto i : vec)
//{
// cout << i << endl;
// }
cout << s.strStr("", "a") << endl;
cout << s.strStr("a", "") << endl;
cout << s.strStr("", "") << endl;
cout << s.strStr("helloworld", "world") << endl;
cout << s.strStr("aaa", "aa") << endl;
cout << s.strStr("mississippi", "issip") << endl;
cout << s.strStr("aaabaaabbbabaa", "babb") << endl;
}