CodeBlanks Logo Icon CodeBlanks

Meydan okuma 5

Nihaimini görev: tüm modülleri birleştirirsin.

Meydan okuma 5 - Birikimli final

Beşinci ve son meydan okuma, önceki tüm fonksiyonları birleştirir. count_doublings, kmp_search, convex_hull ve ford_fulkerson yukarıda hazırdır.

Sen count_matches yazacaksın: verilen metin ve örüntü için toplam eşleşme sayısını kmp_search kullanarak bul.

Bu görev, birikimli zincirin son halkasıdır ve uzman seviyesinin finalıdır.

Uygulama count_matches fonksiyonu

count_matches(text, pattern) yaz. kmp_search kullanarak metindeki toplam eşleşme sayısını döndür. print(count_matches("abababab", "ab")) ile 4 yazdır. Diğer dört fonksiyon yukarıda hazırdır.

Başlangıç kodu

solution.py
def count_doublings(n):
    count = 0
    size = 1
    last = 0
    for i in range(n):
        if last == size:
            size *= 2
            count += 1
        last += 1
    return count

def kmp_search(text, pattern):
    n, m = len(text), len(pattern)
    lps = [0] * m
    length = 0
    i = 1
    while i < m:
        if pattern[i] == pattern[length]:
            length += 1
            lps[i] = length
            i += 1
        elif length != 0:
            length = lps[length - 1]
        else:
            lps[i] = 0
            i += 1
    i = j = 0
    while i < n:
        if text[i] == pattern[j]:
            i += 1
            j += 1
            if j == m:
                return i - j
        elif j != 0:
            j = lps[j - 1]
        else:
            i += 1
    return -1

def convex_hull(points):
    pts = sorted(set(points))
    if len(pts) <= 1:
        return pts
    def cross(o, a, b):
        return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
    lower = []
    for p in pts:
        while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0:
            lower.pop()
        lower.append(p)
    upper = []
    for p in reversed(pts):
        while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0:
            upper.pop()
        upper.append(p)
    return lower[:-1] + upper[:-1]

def ford_fulkerson(capacity, source, sink):
    from collections import defaultdict
    graph = defaultdict(dict)
    for u, v, c in capacity:
        graph[u][v] = graph[u].get(v, 0) + c
        graph[v][u] = graph[v].get(u, 0)
    max_flow = 0
    def dfs(s, t, flow, visited):
        if s == t:
            return flow
        visited.add(s)
        for v in graph[s]:
            if v not in visited and graph[s][v] > 0:
                pushed = dfs(v, t, min(flow, graph[s][v]), visited)
                if pushed > 0:
                    graph[s][v] -= pushed
                    graph[v][s] = graph[v].get(s, 0) + pushed
                    return pushed
        return 0
    while True:
        f = dfs(source, sink, float('inf'), set())
        if f == 0:
            break
        max_flow += f
    return max_flow

def count_matches(text, pattern):
    # Tüm eşleşme sayısını bul

Örnek çözüm

solution.py
def kmp_search(text, pattern):
    n, m = len(text), len(pattern)
    lps = [0] * m
    length = 0
    i = 1
    while i < m:
        if pattern[i] == pattern[length]:
            length += 1
            lps[i] = length
            i += 1
        elif length != 0:
            length = lps[length - 1]
        else:
            lps[i] = 0
            i += 1
    i = j = 0
    while i < n:
        if text[i] == pattern[j]:
            i += 1
            j += 1
            if j == m:
                return i - j
        elif j != 0:
            j = lps[j - 1]
        else:
            i += 1
    return -1
def count_matches(text, pattern):
    total = 0
    pos = 0
    while pos < len(text):
        idx = kmp_search(text[pos:], pattern)
        if idx == -1:
            break
        total += 1
        pos += idx + 1
    return total
print(count_matches("abababab", "ab"))

Beklenen çıktı

4

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 eşleşme arama

Soru
idx = kmp_search(text[____:], pattern)
Cevap
idx = kmp_search(text[pos:], pattern)

Açıklama: pos'tan itibaren ara.

2 count_matches hangi fonksiyonu kullanır?

  • kmp_search Doğru cevap
  • convex_hull
  • ford_fulkerson
  • count_doublings

Açıklama: count_matches, kmp_search kullanarak eşleşme sayar.

3 Birikimli zincir

Kavram Anlamı
1. adım count_doublings
2. adım kmp_search
3. adım convex_hull
4. adım ford_fulkerson

Uzman seviye bölümündeki tüm dersler