notes34

notes34 - cs 251 String Matching -implementing c function...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
cs 251 String Matching -implementing c function strstr - searching for a substring in a string Brute Force char *strstr( char *haystack, char *needle ) { char *s = haystack; while (*s != '\0') { char *h = s; char *l = needle; h++; l++; } if (*l == 0) { //end of needle //we have a match return s; } s++; } return NULL; //no math } complexity: ======== M = length of needle N = length of haystack complexity = O( MN ) Worst case: haystack="AAAAAAAA" needle="AAAH" - lots of unnecessary, repetitive searches Rabin Karp String Matching -uses a hash function applied to M character sequence -compares hash values in the text and the parent instead of comparing char by char AAAAAAAA = 225 AAH = 34 -we compute the hash number in O(M) by adding the ascii value of each char -if we need to compute the hash value in the text of N we will need N( O(M) ) time to compute the hash - However , if we use the hash function that only adds the ascii values, then we can obtain the next hash value by subtracting the letter before the next position and adding
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

notes34 - cs 251 String Matching -implementing c function...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online