This commit is contained in:
youngyangyang04
2020-07-16 09:13:18 +08:00
parent 5f92ccabed
commit ad7e958509

View File

@ -15,10 +15,12 @@ public:
next[0] = -1; next[0] = -1;
int j = -1; int j = -1;
for(int i = 1; i < s.size(); i++){ for(int i = 1; i < s.size(); i++){
while(j >= 0 && s[i] != s[j+1]) while (j >= 0 && s[i] != s[j + 1]) {
j = next[j]; j = next[j];
if(s[i] == s[j+1]) }
if (s[i] == s[j + 1]) {
j++; j++;
}
next[i] = j; next[i] = j;
} }
} }
@ -37,7 +39,7 @@ public:
if (haystack[i] == needle[j + 1]) { if (haystack[i] == needle[j + 1]) {
j++; j++;
} }
if (j == (needle.size() -1) ) { if (j == (needle.size() - 1) ) {
return (i - needle.size() + 1); return (i - needle.size() + 1);
} }
} }