-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import random | ||
|
||
|
||
def sift_up(data, i): | ||
if i == 0: | ||
return | ||
parent = (i - 1) // 2 | ||
if data[parent] > data[i]: | ||
data[parent], data[i] = data[i], data[parent] | ||
sift_up(data, parent) | ||
|
||
|
||
def sift_down(data, i): | ||
child1 = i * 2 + 1 | ||
child2 = i * 2 + 2 | ||
if child1 >= len(data): | ||
return | ||
if child2 >= len(data): | ||
child_min = child1 | ||
else: | ||
child_min = child1 if data[child1] < data[child2] else child2 | ||
if data[child_min] < data[i]: | ||
data[i], data[child_min] = data[child_min], data[i] | ||
sift_down(data, child_min) | ||
|
||
|
||
def heapify(data): | ||
for i in range(len(data) - 1, -1, -1): | ||
sift_down(data, i) | ||
|
||
|
||
def heappush(data, x): | ||
data.append(x) | ||
sift_up(data, len(data) - 1) | ||
|
||
|
||
def heappop(data, i=0): | ||
data[i], data[-1] = data[-1], data[i] | ||
res = data.pop() | ||
sift_up(data, i) | ||
sift_down(data, i) | ||
return res | ||
|
||
|
||
if __name__ == '__main__': | ||
n = random.randint(0, 100) | ||
h = [random.randint(0, 1000) for i in range(n)] | ||
|
||
heapify(h) | ||
while len(h) > 0: | ||
print(heappop(h)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
def find_substrings_naive(s, p): | ||
N = len(s) | ||
K = len(p) | ||
substrings = [] | ||
for i in range(N - K + 1): | ||
if all([s[i + j] == p[j] for j in range(K)]): | ||
substrings.append(i) | ||
return substrings | ||
|
||
|
||
def prefix_function(s): | ||
d = [0] * (len(s) + 1) | ||
for i in range(2, len(d)): | ||
d[i] = d[i - 1] | ||
while s[i - 1] != s[d[i]] and d[i] > 0: | ||
d[i] = d[d[i]] | ||
if s[i - 1] == s[d[i]]: | ||
d[i] += 1 | ||
return d | ||
|
||
|
||
def find_substrings_kmp(s, p): | ||
substrings = [] | ||
d = prefix_function(p + '$' + s) | ||
for i in range(len(p) + 1, len(d)): | ||
if d[i] == len(p): | ||
substrings.append(i - 2 * len(p) - 1) | ||
return substrings | ||
|
||
|
||
def find_substrings_native(s, p): | ||
i = -1 | ||
substrings = [] | ||
while True: | ||
i = s.find(p, i + 1) | ||
if i >= 0: | ||
substrings.append(i) | ||
else: | ||
return substrings | ||
|
||
from time import time | ||
if __name__ == '__main__': | ||
#s = input().strip() | ||
#p = input().strip() | ||
s = 'a' * 100000 + 'b' | ||
p = 'a' * 50000 + 'b' | ||
|
||
t1 = time() | ||
print('Python: ', ' '.join(map(str, find_substrings_native(s, p)))) | ||
t2 = time() | ||
#print('Naive : ', ' '.join(map(str, find_substrings_naive(s, p)))) | ||
t3 = time() | ||
print('KMP : ', ' '.join(map(str, find_substrings_kmp(s, p)))) | ||
t4 = time() | ||
print(f'{t2 - t1:.8f}, {t3 - t2:.8f}, {t4 - t3:.8f}') |