-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrstr.cpp
47 lines (43 loc) · 1.15 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Solution {
public:
bool search_next(std::string::iterator it0, std::string::iterator it1, std::string::iterator hayend, std::string::iterator needleend){
if(it1 == needleend)
return true;
if(it0==hayend)
return false;
bool tmp= false;
if(*it1 == *it0){
tmp = search_next(it0+1, it1+1, hayend, needleend);
}
return tmp;
}
int strStr(std::string &haystack, std::string &needle) {
if(needle.length()==0)
return 0;
if(needle.length()>haystack.length())
return -1;
if(!haystack.compare(needle))
return 0;
bool tmp = false;
for(std::string::iterator it=haystack.begin(); it!=haystack.end(); it++){
if(*it==needle.front()){
bool tmp = search_next(it+1, needle.begin()+1, haystack.end(), needle.end());
if(tmp==true)
return it-haystack.begin();
}
}
return -1;
}
};
int main(){
Solution s;
std::string haystack("mississippi");
std::cout<< "haystack:" <<haystack <<std::endl;
std::string needle("issip");
std::cout<< "needle:" <<needle <<std::endl;
std::cout<<s.strStr(haystack, needle)<<std::endl;
}