Created
January 18, 2016 02:55
-
-
Save bowbowbow/9fd243c6e348272a1608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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