CS300-11_String_Matc - String Matching Sung Yong Shin TC Lab CS Dept KAIST Contents 1 Problem Definition 2 Straightforward Algorithm 3 String

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: String Matching Sung Yong Shin TC Lab. CS Dept., KAIST Contents 1. Problem Definition 2. Straightforward Algorithm 3. String Matching with Finite Automaton 4. Knuth-Morris-Pratt Algorithm 5. Boyer-Moore Algorithm 1. Problem Definition • SM : Given a text string and another string called a pattern , find the first occurrence of the pattern in the text. P--- → BOOK ↓↓↓↓ T THIS BOOK IS MINE. 123456789… ↑ • what to report the index in T where a copy of P begins first, if P occurs in T . the length of T + 1, otherwise T P Straightforward Algorithm Let | T | = n and | P | = m example. T A B A B A B C C A P A B A B D B A A B D A B A B D worst case. P:AAAB T:AAAAA…… O(m(n-m)) -----> too much time! 1 n m n-m+1 T … Slide P one place to the right whenever failed ! How many times to slide ? (n-m) times. 1 2 3 4 5 6 7 8 9 T 1 2 3 4 5 6 7 8 9 function Match ( P , T : String ) : index ; var i, j , k : index ; { i : the current guess at which P begins in T ; j : the index of the current character in T ; k : the index of the current character in P . } begin i := 1; j := 1; k := 1; while j ≤ T.length and k ≤ P.length do if t j = p k then j := j + 1; k := k + 1; else { slide pattern forward and start over } i := i +1; j := i ; k := 1 end { if } end { while } if k > P.length then Match := i { match found } else Match := j { j = T.length + 1, no match } end { if } end { Match } 1 n i+m-1 T j k m 1 m P i String Matching with Finite Automaton T A B A A C A A B A A A B C A A C P A A B C A A B C A A B C A A B C A A B C NO BACKING UP IN THE TEXT !!! start 1 2 3 4 * C B B C A A B C B A A C Σ = { A, B, C } • Finite Automaton (S, ∑, δ , S , F) final state initial state state transition function input symbols finite control states δ :S × I → S start 1 2 3 4 * C B B C A A B C B A A C the finite automaton for P=‘AABC’ S B A C input tape finite state control head •...
View Full Document

This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

Page1 / 27

CS300-11_String_Matc - String Matching Sung Yong Shin TC Lab CS Dept KAIST Contents 1 Problem Definition 2 Straightforward Algorithm 3 String

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

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