-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementstrStr().h
56 lines (55 loc) · 1.21 KB
/
ImplementstrStr().h
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
/**
*Solution 1
*time:O(n*m) space:O(1)
*暴力法
*/
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int n=strlen(haystack),m=strlen(needle);
for(int i=0;i<=n-m;++i)
{
int j;
for(j=0;j<m;++j)
{
if(*(haystack+i+j)!=*(needle+j))break;
}
if(j==m)return haystack+i;
}
return nullptr;
}
};
/**
*Solution 2
*time:O(n+m) space:O(m)
*KMP
*/
clas
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int ind=kmp(haystack,needle);
if(ind==-1)return nullptr;
else return haystack+ind;
}
private:
int kmp(char *str,char *pattern)
{
int n=strlen(str),m=strlen(pattern);
if(m==0)return 0;
vector<int> pi(m,-1);
for(int i=-1,j=1;j<m;++j)
{
while(i>-1&&*(pattern+j)!=*(pattern+i+1))i=pi[i];
if(*(pattern+j)==*(pattern+i+1))++i;
pi[j]=i;
}
for(int i=-1,j=0;j<n;++j)
{
while(i>-1&&*(pattern+i+1)!=*(str+j))i=pi[i];
if(*(pattern+i+1)==*(str+j))++i;
if(i==m-1)return j-i;
}
return -1;
}
};