Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Created January 17, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bowbowbow/a7e19689481d42b28a42 to your computer and use it in GitHub Desktop.
Save bowbowbow/a7e19689481d42b28a42 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> getPi(string p){
int m = (int)p.size(), j=0;
vector<int> pi(m, 0);
for(int i = 1; i< m ; i++){
while(j > 0 && p[i] != p[j])
j = pi[j-1];
if(p[i] == p[j])
pi[i] = ++j;
}
return pi;
}
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;
}
int main(){
string s, p;
getline(cin, s);
getline(cin, p);
auto matched = kmp(s,p);
printf("%d\n", (int)matched.size());
for(auto i : matched)
printf("%d ", i+1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment