1 / 7

התאמת מחרוזות

for I = 0 … N-M for J = 1 … M if T(I+J) != P(J) next I exit “shift is”, I print FAIL. קלט: מחרוזת T(1)…T(N) תבנית P(1)…P(M) פלט: אינדקס I כך שעבור J=1..M : P(J) = T(I+J) ( או שאין כזה ). התאמת מחרוזות. J = 1 ; K = 1 // I = 0 while K <= N

topper
Download Presentation

התאמת מחרוזות

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. for I = 0 … N-M for J = 1 … M if T(I+J) != P(J) next I exit “shift is”, I print FAIL קלט: מחרוזת T(1)…T(N) תבנית P(1)…P(M) פלט: אינדקס I כךשעבור J=1..M : P(J) = T(I+J) (או שאין כזה) התאמת מחרוזות

  2. J = 1 ; K = 1 // I = 0 while K <= N if T(K) = P(J) K++ ; J++ // I stays if J > M exit “shift is”, K-J // I else if J = 1 { K++ } // I++ else { J=1 } // I += J-1 T = BCABCEABCDF P = ABCD BCABCEABCDF A A ABCD A אם כל אותיות P שונות זו מזו...

  3. הגדרה: next(q) = max d (d < q) כך ש: P(1…d) = P(d+q-l…q) q 1 2 3 4 5 P ABABC next 0 0 1 2 0 T = CABABABAC P = ABABC CABABABAC A ABABC ABABAC * ואם יש “חזרות” ב-P...

  4. J = 1 ; K = 1 // I = 0 while K <= N if T(K) = P(J) K++ ; J++ // I stays if J > M exit “shift is”, K-J // I else // T(K) != P(J) if J = 1 K++ // I++ else J = next(J-1)+1 // I +=J-1-next(J-1) // K stays אלגוריתם יעיל (בהינתן next)

  5. משפט: האלגוריתם רץ בזמן O(N) למה: בכל לולאה, או ש-K גדל, או ש-I גדל (ואף אחד מהם לא קטן). ניתוח זמן ריצה

  6. q=7: next(6) = 4 ABABAB|C| ABAB|A|BC next(4) = 2 AB|A|BABC next(2) = 0 ==> next(7) = 0 q 1 2 3 4 5 6 7 P = ABABABC next: 0 0 1 2 3 q=6 : next(5) = 3 ABABA|B|C ABA|B|ABC ==> next(6) = 4 דוגמת חישוב next

  7. next(1) = 0 K = 1 for q = 2 … M K = q repeat K = next(K-1) + 1 until P(K) = P(q) or K=1 if P(K) = P(q) next(q) = K else next(q) = 0 נתוח זמן ריצה: I=q-K או q עולים כל לולאה. (K בעצם עולה רק ב-1כש-q עולה.) אלגוריתם לחשוב next

More Related