Jump to content

Talk:Knuth–Morris–Pratt algorithm

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Madoka (talk | contribs) at 03:19, 24 March 2004. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Removed the following text from the article, for I'm not sure if it's correct. It is clear, however, that it's not C, but rather a hybrid between C and Pascal...

The following implementation is written in C:

INPUT: Text T[0,n-1], Pattern P[0,m-1]
Output alle matches of P in T
InitNext(P);
j=0;
for (i=1;i<=n;i++)
{
   while ((j>0) && (P[j+1] != T[i]))
   {
      j=next[j];
   }
   if (P[j+1] == T[i])
      j++;
   if (j == m-1)
   {
      print "Found P";
      j = next[j];
   }
}
Procedure InitNext(P)
Input: Pattern P
next[1] = 0;
for (q=2;q<=m;q++)
{
   while ((l>0) && (P[l+1] != P[q]))
   {
      l = next[l];
   } 
   if (P[l+1] == P[q])
      l++;
   next[q] = l;
}