Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Created January 18, 2016 02:55
Show Gist options
  • Save bowbowbow/9fd243c6e348272a1608 to your computer and use it in GitHub Desktop.
Save bowbowbow/9fd243c6e348272a1608 to your computer and use it in GitHub Desktop.
vector<int> kmp(string s, string p){
vector<int> ans;
auto pi = getPi(p);
int n = (int)s.size(), m = (int)p.size(), j =0;
for(int i = 0 ; i < n ; i++){
while(j>0 && s[i] != p[j])
j = pi[j-1];
if(s[i] == p[j]){
if(j==m-1){
ans.push_back(i-m+1);
j = pi[j];
}else{
j++;
}
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment