-
-
Save jeon3029/709780b452af10914190d217fa14779b to your computer and use it in GitHub Desktop.
This file contains 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
#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