Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define PB push_back
- #define ZERO (1e-10)
- #define INF (1<<30)
- #define LINF (1LL<<60)
- #define NINF -INF
- #define CL(A,I) (memset(A,I,sizeof(A)))
- #define add(A,B) (g[A].PB(B),g[B].PB(A))
- #define ADD(A,B,V) (add(A,B),v[A].PB(V),v[B].PB(V))
- #define DEB printf("DEB!\n");
- #define D(X) cout<<" "<<#X": "<<X<<endl;
- #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
- typedef long long ll;
- typedef pair<ll,ll> pll;
- typedef vector<int> vi;
- typedef pair<int,int> ii;
- #define REP(i, n) for (int i = 0; i < n; i++)
- #define F(W) REP(i,W)
- #define FF(W) REP(j,W)
- #define FT(I,W) for(int k(I);k<W;++k)
- #define IN(n) int n; cin >> n;
- #define PR(A,X) (X?A.second:A.first)
- #define MX (1<<22)
- vi o;
- void preKMP(const char *p,int *f){
- f[0]=-1;
- for(int i(1),k;p[i];++i){
- k=f[i-1];
- while(~k&&p[k]!=p[i-1])k=f[k];
- f[i]=k+1;
- }
- }
- void KMP(const char *p,const char *t){
- static int f[MX];
- preKMP(p,f);
- for(int i(0),k(0),m(strlen(p));t[i];)
- if(!~k)++i,k=0;
- else if(t[i]==p[k]){
- ++i;
- if(++k==m)o.PB(i-k),i-=k-1;
- }else k=f[k];
- }
- char p[MX],t[MX];
- int main(void){
- while(~scanf("%*d%s%s",p,t)){
- KMP(p,t);
- for(auto i:o)printf("%d\n",i);
- if(!o.empty())putchar_unlocked('\n');
- o.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement