Cod sursa(job #629387)

Utilizator blue_phoenixPosea Elena blue_phoenix Data 3 noiembrie 2011 11:36:48
Problema Potrivirea sirurilor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include<stdio.h>
#include<string.h>
#define NMAX 200000
#define MMAX 200000



void preKMP(char*x,int m,int KMPshift[])
{
int i,j;
i=0;
j=KMPshift[0]=-1;
while(i<m)
          {
          while(j>-1&&x[i]!=x[j])
                                 j=KMPshift[j];
          ++i;
          ++j;
          if(x[i]==x[j])
                        KMPshift[i]=KMPshift[j];
          else
              KMPshift[i]=j;
          }
}



void KMP(char*x,char*y,int n,int m)
{
int i,j;
int KMPshift[MMAX];
preKMP(y,m,KMPshift);
i=j=0;
while(i<n)
          {
          while((j>-1)&&(x[i]!=y[j]))
                                j=KMPshift[j];
          ++i;++j;
          if(j>=m)
                  {
                  printf("%d ",i-j);
                  j=KMPshift[j];
                  }
          }
}



int main()
{
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
char x[NMAX];
char y[MMAX];
int n,m;
gets(x);
gets(y);
n=strlen(x);
m=strlen(y);
KMP(x,y,n,m);
return 0;
}