Skip to content

Commit

Permalink
generalized to take shared substring among different words
Browse files Browse the repository at this point in the history
git-svn-id: http://hashcollision.org/svn/repos/projects@367 e592b0e5-eb13-0410-a8c4-ffe2ebf9f621
  • Loading branch information
dyoo committed Apr 2, 2007
1 parent 5cdad99 commit daf9c65
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions longest-common-substring.ss
Expand Up @@ -6,12 +6,15 @@
(lib "contract.ss"))

(provide/contract [longest-common-substring
(string? string? . -> . string?)])
(() (listof string?) . ->* . (string?))])


;; FIXME: put this in the suffixtree API
(define (sentinel? ch)
(not (char? ch)))


;; FIXME: put this in the suffixtree API
(define (label-length/removing-sentinel label)
(cond
[(= 0 (label-length label)) 0]
Expand All @@ -23,11 +26,12 @@

;; longest-common-substring: string string -> string
;; Returns the longest common substring between s1 and s2.
(define (longest-common-substring s1 s2)
(define (longest-common-substring . words)
(let-values ([(tree labels annotations)
(make-tree/annotations (list s1 s2))])
(local ((define (shared? node)
(= 2 (length (hash-table-get annotations node))))
(make-tree/annotations words)])
(local ((define N (length words))
(define (shared? node)
(= N (length (hash-table-get annotations node))))
(define best-so-far '())
(define best-so-far-length 0))
;; loop: node (listof label) number -> void
Expand Down Expand Up @@ -69,7 +73,8 @@
;; that node belongs to.
(define (annotate-tree a-tree original-labels)
(local ((define ht (make-hash-table))


;; FIXME: optimize using a hash table lookup structure.
(define (label->original-label l)
(find (lambda (o) (label-source-eq? l o))
original-labels)))
Expand Down

0 comments on commit daf9c65

Please sign in to comment.