Re: divide & conquer algorithm to implement string matching
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 29 May 2007 12:58:10 +0000
misterio said:
I'm trying to find a divide & conquer algorithm to implement a string
matching program that can find occurrences of a string like "0110" in
a string like "01101011001100110110".Does anybody has any idea?
The problem you have with divide-and-conquer in this case is that it can
cause you to miss some matches. For instance, if you take your specific
example, a naive d&c algorithm would do this:
01101 01100 11001 10110
^^^^ ^^^^ ^^^^
match match miss match
which is great, because it gives you three matches, but...
01101011001100110110
^^^^^^^^
....misses these two.
In any case, d&c doesn't really buy you anything here. You could just do
this:
0110 1011 0011 0011 0110
unsigned long haystack = 0x6B336;
unsigned char needle = 0x6;
unsigned char mask = 0xF;
int i = 0;
int c = 0;
while(haystack >= needle)
{
if((haystack & mask) == needle)
{
printf("Match %d at position %d\n", ++c, i);
}
++i;
haystack >>= 1;
}
That is, you can simply shift your way through the bits, masking as you
go.
Maybe I'm missing something, but I really don't see how d&c works for
you in this situation.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- Follow-Ups:
- Re: divide & conquer algorithm to implement string matching
- From: misterio
- Re: divide & conquer algorithm to implement string matching
- References:
- divide & conquer algorithm to implement string matching
- From: misterio
- divide & conquer algorithm to implement string matching
- Prev by Date: divide & conquer algorithm to implement string matching
- Next by Date: Re: divide & conquer algorithm to implement string matching
- Previous by thread: divide & conquer algorithm to implement string matching
- Next by thread: Re: divide & conquer algorithm to implement string matching
- Index(es):
Relevant Pages
|