ENع
→Naive search
الدرس 46 · Strings and problem-solving patterns

Naive string search

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

Finding a word inside a text is one of the most common jobs a program does. The simplest way slides the pattern along the text one place at a time and compares it character by character. Watch it find "ABC" in "ABABCABC" with 12 comparisons, then watch a repetitive text push it to its worst case of (n − m + 1)·m. The Software lessons "Strings: text in memory" and "Searching: linear and binary" cover how strings are stored and what a linear scan is; this lesson builds on both.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 46 · تفاعلي
01 / 09

Find a pattern in text

Find every place the pattern p = "ABC" occurs in the text t = "ABABCABC". Positions count from 0. Here n = 8 is the text length and m = 3 the pattern length. A shift s means: put p under t starting at t[s].

t = "ABABCABC"   n = 8
p = "ABC"        m = 3

index  0 1 2 3 4 5 6 7
t      A B A B C A B C

shifts to try: s = 0 .. n - m = 0 .. 5
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Find a pattern in text

    Find every place the pattern p = "ABC" occurs in the text t = "ABABCABC". Positions count from 0. Here n = 8 is the text length and m = 3 the pattern length. A shift s means: put p under t starting at t[s].

    t = "ABABCABC"   n = 8
    p = "ABC"        m = 3
    
    index  0 1 2 3 4 5 6 7
    t      A B A B C A B C
    
    shifts to try: s = 0 .. n - m = 0 .. 5
  2. Shift 0: compare left to right

    Start at s = 0. Compare one character pair at a time: A = A, B = B, then A against C. They differ, so stop. Shift 0 is not a match, and it cost 3 comparisons. Each comparison drops a block on the tally.

    s = 0
      j = 0  t[0] A  vs  p[0] A   equal
      j = 1  t[1] B  vs  p[1] B   equal
      j = 2  t[2] A  vs  p[2] C   differ: stop
    3 comparisons, no match
  3. Slide one place right

    Slide the pattern one place: s = 1. The first pair is B against A. They differ at once, so this shift costs just 1 comparison. The two matches we saw at shift 0 are simply forgotten.

    s = 1
      j = 0  t[1] B  vs  p[0] A   differ: stop
    1 comparison, no match
    running total: 4
  4. Shift 2: a full match

    At s = 2 all three pairs agree: A = A, B = B, C = C. The while loop runs off the end with j = 3 = m, so 2 goes into hits. A full match always costs m = 3 comparisons.

    s = 2
      j = 0  t[2] A  vs  p[0] A   equal
      j = 1  t[3] B  vs  p[1] B   equal
      j = 2  t[4] C  vs  p[2] C   equal
    j == m: hits = [2]
    running total: 7
  5. Quick misses at 3 and 4

    At s = 3 the first pair is B against A; at s = 4 it is C against A. Both fail on the first comparison, so each shift costs only 1. Most shifts in ordinary text end this fast.

    s = 3
      j = 0  t[3] B  vs  p[0] A   differ
    s = 4
      j = 0  t[4] C  vs  p[0] A   differ
    running total: 9
  6. The last shift: n − m

    s = 5 is the last shift, because the pattern must fit: n − m = 8 − 3 = 5. That is why the loop uses range(n − m + 1). All three agree, so hits = [2, 5]. Done.

    s = 5 = n - m (the last shift that fits)
      t[5] A = A, t[6] B = B, t[7] C = C
    hits = [2, 5]
    range(n - m + 1) = range(6) = 0 .. 5
  7. Count the comparisons

    The tally holds 3 + 1 + 3 + 1 + 1 + 3 = 12 comparisons. 6 shifts, each costing 1 to m = 3, so never more than (n − m + 1)·m = 18.

    comparisons per shift
      s     0  1  2  3  4  5
      cost  3  1  3  1  1  3
    total 12
    
    best case  : n - m + 1     = 6
    worst case : (n - m + 1)·m = 18
  8. The worst case

    Now t = "AAAAAAAA" and p = "AAB". At every shift the two A's agree and only the last pair fails. Every shift costs the full m = 3, and the tally fills up: 6 × 3 = 18 comparisons, no matches.

    t = "AAAAAAAA"   p = "AAB"
    every shift: A = A, A = A, A vs B differ
    per shift  3  3  3  3  3  3
    total 18 = (n - m + 1)·m
    hits = []
  9. How bad can it get?

    So the worst case is Θ((n − m + 1)·m), which is O(nm). For a 1,000,000-character text and a 1,000-character pattern that is about a billion comparisons. The waste: it never reuses what a failed shift already learned.

    worst case  (n - m + 1)·m  = Θ((n - m + 1)·m) = O(nm)
    n = 1,000,000, m = 1,000
      (n - m + 1)·m = 999,001,000
    best case   n - m + 1 (first pair fails every time)
    extra space O(1) besides the hits list

اختبار يتاح بعد الخطوة الأخيرة.