Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define all(x) x.begin(),x.end()
- vector<int> prefix_function(string &s){
- int n=s.size();
- vector<int>pi(n);
- for(int i=1;i<n;i++){
- int j=pi[i-1];
- while(j and s[i]!=s[j]) j=pi[j-1];
- if(s[i]==s[j]) j++;
- pi[i]=j;
- }
- return pi;
- }
- void KMP(string pat, string text){
- int n=pat.size();
- pat+='#';
- pat+=text;
- vector<int>pref = prefix_function(pat);
- for(int i=0;i<(int)pref.size();i++){
- if(pref[i]==n){
- cout<<i-2*n<<" ";
- }
- }
- }
- void init() {
- string pat,text;
- cin>>text>>pat;
- KMP(pat,text);
- }
- int32_t main() {
- init();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement