1 / 11

String Matching

String Matching. By Joshua Yudaken. Terms. Haystack A string in which to search Needle The string being searched for find the needle in the haystack. Basic Algorithm. For each letter in the haystack Check if the needle is present

kenyar
Download Presentation

String Matching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Matching By Joshua Yudaken

  2. Terms • Haystack • A string in which to search • Needle • The string being searched for • find the needle in the haystack

  3. Basic Algorithm • For each letter in the haystack • Check if the needle is present for (int k = 0; k < (strlen(haystack) - strlen(needle) + 1); k++) if (memcmp(haystack,needle,strlen(needle)) == 0) return true; return false;

  4. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | BARBER

  5. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | | BARBER | BARBER

  6. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | | | BARBER | | BARBER | BARBER

  7. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | | | | BARBER | | | BARBER | | BARBER | BARBER

  8. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | | | | | BARBER | | | | BARBER | | | BARBER | | BARBER | BARBER

  9. Boyer-Moore Algorithm Haystack = “jim saw it at a barbershop” Needle = “barber” JIM SAW IT AT THE BARBERSHOP | | | | | BARBER | | | | BARBER | | | BARBER | | BARBER | BARBER

  10. Space/Time Tradeoff • Create a ‘shift table’ for the search For “barber”: [a: 4, b: 2, c: 6, d: 6, e: 1,…,r: 3,…, z: 6, ‘ ‘: 6] • Set every element in the shift table to the length of the needle • Go through each letter (from the second last to the first) of the needle • If the distance from the letter to the end of the needle, is less than the letters current value in the shift table • update the shift table with the distance to the end of the needle 3 1 2 3 4 2

  11. Best when • The “alphabet” used in the haystack is much larger than that of the needle. • The haystack is long • The same needle is to be used in many different searches • In other cases, use strstr()! • Or a different available function.

More Related