String Matching - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

String Matching

Description:

String Matching. By Joshua Yudaken. Terms. Haystack. A string in which to search. Needle ... for (int k = 0; k (strlen(haystack) - strlen(needle) 1); k ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 12
Provided by: olympiad
Category:

less

Transcript and Presenter's Notes

Title: String Matching


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 lt (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.
Write a Comment
User Comments (0)
About PowerShow.com