Advertisement
Morass

NHAY

Jul 24th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF (1<<30)
  6. #define LINF (1LL<<60)
  7. #define NINF -INF
  8. #define CL(A,I) (memset(A,I,sizeof(A)))
  9. #define add(A,B) (g[A].PB(B),g[B].PB(A))
  10. #define ADD(A,B,V) (add(A,B),v[A].PB(V),v[B].PB(V))
  11. #define DEB printf("DEB!\n");
  12. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  13. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  14. typedef long long ll;
  15. typedef pair<ll,ll> pll;
  16. typedef vector<int> vi;
  17. typedef pair<int,int> ii;
  18. #define REP(i, n) for (int i = 0; i < n; i++)
  19. #define F(W) REP(i,W)
  20. #define FF(W) REP(j,W)
  21. #define FT(I,W) for(int k(I);k<W;++k)
  22. #define IN(n) int n; cin >> n;
  23. #define PR(A,X) (X?A.second:A.first)
  24. #define MX (1<<22)
  25. vi o;
  26. void preKMP(const char *p,int *f){
  27.     f[0]=-1;
  28.     for(int i(1),k;p[i];++i){
  29.         k=f[i-1];
  30.         while(~k&&p[k]!=p[i-1])k=f[k];
  31.         f[i]=k+1;
  32.     }
  33. }
  34. void KMP(const char *p,const char *t){
  35.     static int f[MX];  
  36.     preKMP(p,f);
  37.     for(int i(0),k(0),m(strlen(p));t[i];)
  38.         if(!~k)++i,k=0;
  39.         else if(t[i]==p[k]){
  40.             ++i;
  41.             if(++k==m)o.PB(i-k),i-=k-1;
  42.         }else k=f[k];
  43. }
  44. char p[MX],t[MX];
  45. int main(void){
  46.     while(~scanf("%*d%s%s",p,t)){
  47.         KMP(p,t);
  48.         for(auto i:o)printf("%d\n",i);
  49.         if(!o.empty())putchar_unlocked('\n');
  50.         o.clear();
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement