You are on page 1of 1

visit-www.Codes2u.

com
#include<stdio.h>
#include<conio.h>
void main()
{ int t[100] ,p[100],pi[100],i,k,m,n,q;
printf("\n enter the size of text");
scanf("%d",&n);
printf("\n enter the text");
for(i=1;i<=n;i++)
{
scanf("%d",&t[i]);
}
printf("\n enter the pattern's size to be matched");
scanf("%d",&m);
printf("\n enter the pattern to be matched");
for(i=1;i<=m;i++)
{
scanf("%d",&p[i]);
}
pi[1]=0;
k=0;
for(q=2;q<=m;q++)
{ while(k>0 && p[k+1]!=p[q])
{ k=pi[k];
}
if(p[k+1]==p[q])
{ k++;
}
pi[q]=k;
}
q=0;
for(i=1;i<=n;i++)
{ while(q>0 && p[q+1]!=t[i])
{ q=pi[q];
}
if(p[q+1]==t[i])
{ q++;
}
if(q==m)
{ printf("\n the pattern matches at shift=%d", i-m);
q=pi[q] ;
}
}
getch();
}

You might also like