upd
This commit is contained in:
15
algorithms/labs/lab8/code/comp/1.py
Normal file
15
algorithms/labs/lab8/code/comp/1.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def choose_search_strategy(n, q):
|
||||
cost_linear = q * n
|
||||
cost_binary = n * (n.bit_length()) + q * (n.bit_length())
|
||||
cost_hash = n + q
|
||||
if cost_linear <= cost_binary and cost_linear <= cost_hash:
|
||||
return "linear"
|
||||
if cost_binary <= cost_hash:
|
||||
return "binary_search_with_sort"
|
||||
return "hash_table"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(choose_search_strategy(10**3, 1))
|
||||
print(choose_search_strategy(10**5, 100))
|
||||
print(choose_search_strategy(10**6, 10**6))
|
||||
61
algorithms/labs/lab8/code/comp/2.py
Normal file
61
algorithms/labs/lab8/code/comp/2.py
Normal file
@@ -0,0 +1,61 @@
|
||||
def longest_common_len_naive(strs, k):
|
||||
s0 = strs[0]
|
||||
best = 0
|
||||
for i in range(len(s0)):
|
||||
for j in range(i + 1, len(s0) + 1):
|
||||
sub = s0[i:j]
|
||||
cnt = 0
|
||||
for s in strs:
|
||||
if sub in s:
|
||||
cnt += 1
|
||||
if cnt >= k and j - i > best:
|
||||
best = j - i
|
||||
return best
|
||||
|
||||
|
||||
def longest_common_len_optimized(strs, k):
|
||||
import sys
|
||||
|
||||
def has_len(L):
|
||||
if L == 0:
|
||||
return True
|
||||
base = 911382323
|
||||
mod = 10**9 + 7
|
||||
powL = pow(base, L - 1, mod)
|
||||
commons = None
|
||||
for s in strs:
|
||||
if L > len(s):
|
||||
return False
|
||||
h = 0
|
||||
st = set()
|
||||
for t in range(L):
|
||||
h = (h * base + ord(s[t])) % mod
|
||||
st.add(h)
|
||||
for t in range(L, len(s)):
|
||||
h = (h - ord(s[t - L]) * powL) % mod
|
||||
h = (h * base + ord(s[t])) % mod
|
||||
st.add(h)
|
||||
if commons is None:
|
||||
commons = st
|
||||
else:
|
||||
commons &= st
|
||||
if not commons:
|
||||
return False
|
||||
return len(commons) >= k
|
||||
|
||||
lo, hi = 0, min(len(s) for s in strs)
|
||||
ans = 0
|
||||
while lo <= hi:
|
||||
mid = (lo + hi) // 2
|
||||
if has_len(mid):
|
||||
ans = mid
|
||||
lo = mid + 1
|
||||
else:
|
||||
hi = mid - 1
|
||||
return ans
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
strs = ["abracadabra", "cadabracad", "dabrac"]
|
||||
print(longest_common_len_naive(strs, 2))
|
||||
print(longest_common_len_optimized(strs, 2))
|
||||
12
algorithms/labs/lab8/code/comp/3.py
Normal file
12
algorithms/labs/lab8/code/comp/3.py
Normal file
@@ -0,0 +1,12 @@
|
||||
def choose_mm_algorithm(n):
|
||||
if n <= 64:
|
||||
return "classical"
|
||||
if n <= 512:
|
||||
return "blocked"
|
||||
return "strassen"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(choose_mm_algorithm(64))
|
||||
print(choose_mm_algorithm(256))
|
||||
print(choose_mm_algorithm(1500))
|
||||
Reference in New Issue
Block a user