From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: Chris Hecker <checker@d6.com>
Cc: gerd@gerd-stolpmann.de, eijiro_sumii@anet.ne.jp,
caml-list@inria.fr, sumii@venus.is.s.u-tokyo.ac.jp
Subject: Re: substring match like "strstr"
Date: Tue, 12 Dec 2000 13:28:32 +0100 (MET) [thread overview]
Message-ID: <14902.6640.301542.507040@pc803> (raw)
In-Reply-To: <4.3.2.7.2.20001211202018.00b3f510@shell16.ba.best.com>
Hello,
I missed the beginning of the discussion, but implementing
Knuth-Morris-Pratt is quite easy. The code is given below (26 lines),
following Sedgewick's book "Algorithms". Notice that the function kmp
can be partially applied to a pattern, therefore computing the offset
table only once. The complexity is N+M where N is the size of the text
and M the size of the pattern.
(The following program was proved correct in the Coq Proof Assistant;
without the Not_found exception, however)
Hope this helps,
--
Jean-Christophe FILLIATRE
mailto:Jean-Christophe.Filliatre@lri.fr
http://www.lri.fr/~filliatr
======================================================================
let initnext p =
let m = String.length p in
let next = Array.create m 0 in
let i = ref 1 and j = ref 0 in
while !i < m-1 do
if p.[!i] = p.[!j] then begin
incr i; incr j; next.(!i) <- !j
end else
if !j = 0 then begin incr i; next.(!i) <- 0 end else j := next.(!j)
done;
next
let kmp p =
let next = initnext p in
fun a ->
let n = String.length a
and m = String.length p
and i = ref 0
and j = ref 0 in
while !j < m & !i < n do
if a.[!i] = p.[!j] then begin
incr i; incr j
end else
if !j = 0 then incr i else j := next.(!j)
done;
if !j >= m then !i-m else raise Not_found
======================================================================
next prev parent reply other threads:[~2000-12-14 18:00 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-12-08 6:04 eijiro_sumii
2000-12-08 12:57 ` Gerd Stolpmann
2000-12-10 13:16 ` eijiro_sumii
2000-12-10 15:39 ` Gerd Stolpmann
2000-12-11 3:57 ` eijiro_sumii
2000-12-12 13:58 ` Julian Assange
2000-12-11 21:07 ` Chris Hecker
2000-12-11 22:22 ` Gerd Stolpmann
2000-12-12 5:06 ` Chris Hecker
2000-12-12 12:28 ` Jean-Christophe Filliatre [this message]
2000-12-13 10:02 ` eijiro_sumii
2000-12-13 10:17 ` Eijiro Sumii
2000-12-13 10:53 ` Julian Assange
2000-12-13 13:28 ` Eijiro Sumii
2000-12-12 3:28 ` eijiro_sumii
2000-12-13 1:12 ` John Prevost
2000-12-13 2:35 ` Chris Hecker
2000-12-12 10:07 ` Sven LUTHER
2000-12-14 3:36 ` eijiro_sumii
2000-12-14 6:48 ` Chris Hecker
2000-12-14 8:02 ` eijiro_sumii
2000-12-14 21:53 ` Stephan Tolksdorf
2000-12-14 21:12 Ruchira Datta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=14902.6640.301542.507040@pc803 \
--to=jean-christophe.filliatre@lri.fr \
--cc=caml-list@inria.fr \
--cc=checker@d6.com \
--cc=eijiro_sumii@anet.ne.jp \
--cc=gerd@gerd-stolpmann.de \
--cc=sumii@venus.is.s.u-tokyo.ac.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).