I wrote a KMP sub-string search program in C++. The compilation is oK but the result is not the same as I expected. My code is
class Solution {
public:
static int kmpSearch(const string& s, const string& p){
int* next = new int[p.length()];
getNext(p, next);
int i=0;
int j=0;
while(i<s.length() && j<p.length()){
if(j==-1 || s.at(i) == p.at(j)){
i++; j++;
}else{
j=next[j];
}
}
delete next;
if(j==p.length()) return i-j;
else return -1;
}
static void getNext(string p, int* next){
int len = p.length();
next[0] =-1;
int j=0;
int k=-1;
while(j<len-1){
if(k==-1 || p[j]==p[k]){
j++;
k++;
next[j]=k;
}else{
k=next[k];
}
}
}
};
int main(int argc, char* argv[]){
string s(argv[1]);
string p(argv[2]);
int loc = Solution::kmpSearch(s, p);
std::cout<<" the location is "<< s << " "<< p<< " " <<loc<< std::endl;
}
I used GDB to debug and something strange at condition while(i<s.length() && j<p.length())
in method kmpSearch
. Here is my GDB debug output.
(gdb) l
9
10 int* next = new int[p.length()];
11 getNext(p, next);
12 int i=0;
13 int j=0;
14 while(i<s.length() && j<p.length()){
15 if(j==-1 || s.at(i) == p.at(j)){
16 i++; j++;
17 }else{
18 j=next[j];
(gdb) p j
$24 = -1
(gdb) p p.length()
$25 = 3
(gdb) p j<p.length()
$26 = false
(gdb)
Parameters are shijie jie
. The strange thing is in line 14 that p j<p.length()
should be true, as the j =-1
and p.length is 3, but it prints false