Pagini recente » Cod sursa (job #478125) | Cod sursa (job #2398373) | Cod sursa (job #2769657) | Cod sursa (job #3210200) | Cod sursa (job #629387)
Cod sursa(job #629387)
#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;
}