Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N=100005;
- void KMP(){
- int i,j,table[N],n,m;
- string a,b;
- fflush(stdin); cin>>a; n=a.size();
- fflush(stdin); cin>>b; m=b.size();
- table[0]=0; i=0;
- for(j=1;j<m;++j){
- if(b[i]==b[j]){
- ++i;
- table[j]=i;
- }
- else{
- i=0;
- table[j]=i;
- }
- }
- i=0; j=0;
- while(i<n){
- if(a[i]==b[j]){
- ++j; ++i;
- if(j==m){
- cout<<i-j+1<<" ";
- j=table[j-1];
- }
- }
- else{
- if(j==0) ++i;
- else j=table[j-1];
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(NULL);
- KMP();
- return 0;
- }
Add Comment
Please, Sign In to add comment