From 5eefc0f9c13b9c72a04ee7d21d896b40dd1402a7 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Mon, 8 Feb 2021 09:19:08 +0300 Subject: [PATCH 01/10] First practice --- src/main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 6fd41e1..a72a71f 100644 --- a/src/main.py +++ b/src/main.py @@ -1 +1,13 @@ -# print("Danila_BLudov") \ No newline at end of file + +# inp = "10 3" # input() # 10 3 +# splt = inp.split(" ") +# print(splt) +# res = [] +# for item in splt: +# res.append(int(item)) +# print(res[0]+res[1]) + +# a,b = [int(item) for item in input().split(" ")] +# print(a + b) + +print ("Hello, " + input () + "!") \ No newline at end of file From 4c6b1963d516e4f75c70b6f790f000c2340f5d56 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Mon, 8 Feb 2021 09:36:12 +0300 Subject: [PATCH 02/10] first practice --- src/main.py | 12 ------------ src/main1.py | 7 +++++++ src/main2.py | 2 ++ 3 files changed, 9 insertions(+), 12 deletions(-) create mode 100644 src/main1.py create mode 100644 src/main2.py diff --git a/src/main.py b/src/main.py index a72a71f..3869af0 100644 --- a/src/main.py +++ b/src/main.py @@ -1,13 +1 @@ - -# inp = "10 3" # input() # 10 3 -# splt = inp.split(" ") -# print(splt) -# res = [] -# for item in splt: -# res.append(int(item)) -# print(res[0]+res[1]) - -# a,b = [int(item) for item in input().split(" ")] -# print(a + b) - print ("Hello, " + input () + "!") \ No newline at end of file diff --git a/src/main1.py b/src/main1.py new file mode 100644 index 0000000..cfb2445 --- /dev/null +++ b/src/main1.py @@ -0,0 +1,7 @@ +inp = "10 3" + splt = inp.split(" ") + print(splt) + res = [] + for item in splt: + res.append(int(item)) + print(res[0]+res[1]) \ No newline at end of file diff --git a/src/main2.py b/src/main2.py new file mode 100644 index 0000000..a881a83 --- /dev/null +++ b/src/main2.py @@ -0,0 +1,2 @@ +a,b = [int(item) for item in input().split(" ")] + print(a + b) \ No newline at end of file From 68e4257fd52ad930898e879718ca38f274eb93f6 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Mon, 8 Feb 2021 09:38:54 +0300 Subject: [PATCH 03/10] --- src/main.py | 1 + src/main1.py | 1 + src/main2.py | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/main.py b/src/main.py index 3869af0..1ed0c2b 100644 --- a/src/main.py +++ b/src/main.py @@ -1 +1,2 @@ + print ("Hello, " + input () + "!") \ No newline at end of file diff --git a/src/main1.py b/src/main1.py index cfb2445..5e596a2 100644 --- a/src/main1.py +++ b/src/main1.py @@ -1,4 +1,5 @@ inp = "10 3" + splt = inp.split(" ") print(splt) res = [] diff --git a/src/main2.py b/src/main2.py index a881a83..33a07bf 100644 --- a/src/main2.py +++ b/src/main2.py @@ -1,2 +1,4 @@ a,b = [int(item) for item in input().split(" ")] + + print(a + b) \ No newline at end of file From 38ed248ce46859a76dbf1692db13932b3e06415b Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Mon, 8 Feb 2021 09:53:01 +0300 Subject: [PATCH 04/10] First practic --- src/main.py | 1 - src/main1.py | 3 +-- src/main2.py | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main.py b/src/main.py index 1ed0c2b..3869af0 100644 --- a/src/main.py +++ b/src/main.py @@ -1,2 +1 @@ - print ("Hello, " + input () + "!") \ No newline at end of file diff --git a/src/main1.py b/src/main1.py index 5e596a2..8679b8f 100644 --- a/src/main1.py +++ b/src/main1.py @@ -1,6 +1,5 @@ inp = "10 3" - - splt = inp.split(" ") +splt = inp.split(" ") print(splt) res = [] for item in splt: diff --git a/src/main2.py b/src/main2.py index 33a07bf..a881a83 100644 --- a/src/main2.py +++ b/src/main2.py @@ -1,4 +1,2 @@ a,b = [int(item) for item in input().split(" ")] - - print(a + b) \ No newline at end of file From 592ca27b82198ae0a8cdb4f86e6e214e47dbd1c3 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Mon, 8 Feb 2021 09:55:19 +0300 Subject: [PATCH 05/10] first practic --- src/main.py | 1 + src/main1.py | 1 + src/main2.py | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main.py b/src/main.py index 3869af0..1ed0c2b 100644 --- a/src/main.py +++ b/src/main.py @@ -1 +1,2 @@ + print ("Hello, " + input () + "!") \ No newline at end of file diff --git a/src/main1.py b/src/main1.py index 8679b8f..fa00a43 100644 --- a/src/main1.py +++ b/src/main1.py @@ -1,4 +1,5 @@ inp = "10 3" + splt = inp.split(" ") print(splt) res = [] diff --git a/src/main2.py b/src/main2.py index a881a83..cef70ae 100644 --- a/src/main2.py +++ b/src/main2.py @@ -1,2 +1,3 @@ a,b = [int(item) for item in input().split(" ")] + print(a + b) \ No newline at end of file From da4b322b21871fcedd75e4d2c72a813afb0a4047 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Thu, 11 Feb 2021 17:23:02 +0300 Subject: [PATCH 06/10] First practic --- src/main.py | 1 - src/main1.py | 1 - src/main2.py | 1 - 3 files changed, 3 deletions(-) diff --git a/src/main.py b/src/main.py index 1ed0c2b..3869af0 100644 --- a/src/main.py +++ b/src/main.py @@ -1,2 +1 @@ - print ("Hello, " + input () + "!") \ No newline at end of file diff --git a/src/main1.py b/src/main1.py index fa00a43..8679b8f 100644 --- a/src/main1.py +++ b/src/main1.py @@ -1,5 +1,4 @@ inp = "10 3" - splt = inp.split(" ") print(splt) res = [] diff --git a/src/main2.py b/src/main2.py index cef70ae..a881a83 100644 --- a/src/main2.py +++ b/src/main2.py @@ -1,3 +1,2 @@ a,b = [int(item) for item in input().split(" ")] - print(a + b) \ No newline at end of file From c46b68421cf8f6bd7a346299c7c0bf72ccf5f82a Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Sat, 6 Mar 2021 10:53:18 +0300 Subject: [PATCH 07/10] Danila_Bludov --- src/main3.py | 16 ++++++++++++++++ src/main4.py/main5.py | 16 ++++++++++++++++ src/main6.py | 8 ++++++++ src/main8.py | 11 +++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/main3.py create mode 100644 src/main4.py/main5.py create mode 100644 src/main6.py create mode 100644 src/main8.py diff --git a/src/main3.py b/src/main3.py new file mode 100644 index 0000000..c101085 --- /dev/null +++ b/src/main3.py @@ -0,0 +1,16 @@ +def sort (a , n): + for i in range(0 , n): + for j in range(0 , n-i-1): + if a[j] > a[j + 1]: + a[j + 1] , a[j] = a[j] , a[j + 1]2 + + + +n = "4" #input() +inp = "4 3 2 1" +lst = inp.split(" ") +res = [] +for i in range (0 , n): + res.append(int(lst[i])) + +print(res) \ No newline at end of file diff --git a/src/main4.py/main5.py b/src/main4.py/main5.py new file mode 100644 index 0000000..3089e8f --- /dev/null +++ b/src/main4.py/main5.py @@ -0,0 +1,16 @@ +N = input () +K = input () +q = k.split (" ") +a = [] +for la in q: + a.appended(int(ia)) +N = len(a) +t = 0 +for i in range(0, N-1): + for j in range(0, N-1-i): + if a[j]>a[j+1]: + a[j],a[j+1] = a[j+1],a[j] + e+=1 + print(" ",join(map(str, a))) +if t == 0: + print("0") diff --git a/src/main6.py b/src/main6.py new file mode 100644 index 0000000..a24ec9f --- /dev/null +++ b/src/main6.py @@ -0,0 +1,8 @@ +k = int(input()) +B = [0]*k +for i in range(k): + B[i] = list(map(int, input().split())) +B.sort(key=lambda x: x[0]) +B.sort(key=lambda x: x[1],reverse=1) +for i in range(k): + print(B[i][0], B[i][1]) \ No newline at end of file diff --git a/src/main8.py b/src/main8.py new file mode 100644 index 0000000..33252cc --- /dev/null +++ b/src/main8.py @@ -0,0 +1,11 @@ +size1 = int(input()) +d = list(int(i) for i in input().split(" "))[:size1] +size2 = int(input()) +s = list(int(i) for i in input().split(" "))[:size2] +for i in range(0, size2): + d[s[i]-1] -= 1 +for i in range(0, size1): + if (d[i] < 0): + print("yes") + else: + print("no") \ No newline at end of file From 732655ec54aca330b96d09330ab8f2169cb081c1 Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Sat, 6 Mar 2021 11:06:22 +0300 Subject: [PATCH 08/10] Danila_Bludov --- src/main7.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main7.py diff --git a/src/main7.py b/src/main7.py new file mode 100644 index 0000000..4fc2f86 --- /dev/null +++ b/src/main7.py @@ -0,0 +1,7 @@ +b = {} +s = int(input()) +n = input() +s = n.split() +for i in s: + b[int(i)] = 1 +print(len(b)) From 4ffcf5c627de83ae3c1de826bcb21fe7f0b12bbf Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Sat, 6 Mar 2021 11:32:09 +0300 Subject: [PATCH 09/10] Danila_Bludov --- src/main10.py | 35 +++++++++++++++++++++++++++++++++++ src/main11.py | 33 +++++++++++++++++++++++++++++++++ src/main9.py | 26 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/main10.py create mode 100644 src/main11.py create mode 100644 src/main9.py diff --git a/src/main10.py b/src/main10.py new file mode 100644 index 0000000..3b8c0f1 --- /dev/null +++ b/src/main10.py @@ -0,0 +1,35 @@ +def merge(Q, R): + i = j = 0 + inversions = 0 + res = [] + while i < len(Q) and j < len(R): + if Q[i] <= R[j]: + res.append(Q[i]) + i += 1 + else: + res.append(R[j]) + j += 1 + inversions += len(Q) - i + while i < len(Q): + res.append(Q[i]) + i += 1 + while j < len(R): + res.append(R[j]) + j += 1 + return res, inversions + +def merge_sort(B): + n = len(B) + if n <= 1: + return B, 0 + middle = n//2 + Q, l_inversions = merge_sort(B[0:middle]) + R, r_inversions = merge_sort(B[middle:n]) + res, inversions = merge(Q, R) + return res, inversions + l_inversions + r_inversions + +N = int(input()) +B = list(map(int, input().split(" "))) +B, total_inversions = merge_sort(B) +print(total_inversions) + diff --git a/src/main11.py b/src/main11.py new file mode 100644 index 0000000..bda4050 --- /dev/null +++ b/src/main11.py @@ -0,0 +1,33 @@ +def merge(B, R, left_index, right_index): + i = j = 0 + res = [] + while i < len(B) and j < len(R): + if B[i] <= R[j]: + res.append(B[i]) + i += 1 + else: + res.append(R[j]) + j += 1 + while i < len(B): + res.append(B[i]) + i += 1 + while j < len(R): + res.append(R[j]) + j += 1 + print (f"{left_index} {right_index} {res[0]} {res[len(res) - 1]}") + return res + +def merge_sort(C, left_index, right_index): + n = len(C) + if n <= 1: + return C + middle = n//2 + B = merge_sort(C[0:middle], left_index, left_index+middle-1) + R = merge_sort(C[middle:n], left_index+middle, right_index) + + return merge(B, R, left_index, right_index) + +N = int(input()) +C = list(map(int, input().split(" "))) +c = merge_sort(C, 1, N) +print(" ".join(map(str,c))) diff --git a/src/main9.py b/src/main9.py new file mode 100644 index 0000000..e001b36 --- /dev/null +++ b/src/main9.py @@ -0,0 +1,26 @@ +def radix_sort(B, n): + digit = len(B[0]) + for i in range(digit): + buckets_arr = [[] for j in range(10)] + for item in B: + index = int(item)//10**i % 10 + buckets_arr[index].append(item) + print("Phase", i+1) + B=[] + for i in range(10): + print(f"Bucket {i}: ", end="") + print(", ".join(buckets_arr[i]) if len(buckets_arr[i])>0 else "empty") + B = B + buckets_arr[i] + print("**********") + return B + +N = int(input()) +B = [] +for i in range(N): + B.append(input()) +print("Initial array:") +print(", ".join(B)) +print("**********") +B = radix_sort(B, N) +print("Sorted array:") +print(", ".join(B)) From 8dfc8eadc6d20837cad8095c9b40765a7c931def Mon Sep 17 00:00:00 2001 From: Danila Bludov Date: Thu, 3 Jun 2021 19:47:59 +0300 Subject: [PATCH 10/10] Module 1,2,3,4,5 --- src/bubble_sort.py | 23 +++++++++++++++ src/main10.py | 35 ---------------------- src/main11.py | 33 --------------------- src/main4.py/main5.py | 16 ----------- src/main6.py | 8 ------ src/main7.py | 7 ----- src/main8.py | 11 ------- src/main9.py | 26 ----------------- src/module3/2_1.py | 0 src/module3/2_2.py | 0 src/module3/2_3.py | 0 src/module3/2_4.py | 0 src/module3/2_5.py | 0 src/module3/2_6.py | 0 src/module3/2_7.py | 0 src/module3/3_1.py | 28 ++++++++++++++++++ src/module3/3_2.py | 31 ++++++++++++++++++++ src/module3/3_3.py | 22 ++++++++++++++ src/module3/3_4.py | 18 ++++++++++++ src/module3/4.py | 20 +++++++++++++ src/module3/4_1.py | 14 +++++++++ src/module3/4_2.py | 23 +++++++++++++++ src/module3/4_3.py | 23 +++++++++++++++ src/module3/4_4.py | 18 ++++++++++++ src/module3/5_1.py | 55 +++++++++++++++++++++++++++++++++++ src/module3/5_2.py | 47 ++++++++++++++++++++++++++++++ src/module3/5_3.py | 39 +++++++++++++++++++++++++ src/module3/5_4.py | 48 +++++++++++++++++++++++++++++++ src/module3/5_5.py | 48 +++++++++++++++++++++++++++++++ src/module3/5_6.py | 67 +++++++++++++++++++++++++++++++++++++++++++ src/module3/m3/1/py | 14 +++++++++ src/module3/module3 | 26 +++++++++++++++++ 32 files changed, 564 insertions(+), 136 deletions(-) create mode 100644 src/bubble_sort.py delete mode 100644 src/main10.py delete mode 100644 src/main11.py delete mode 100644 src/main4.py/main5.py delete mode 100644 src/main6.py delete mode 100644 src/main7.py delete mode 100644 src/main8.py delete mode 100644 src/main9.py create mode 100644 src/module3/2_1.py create mode 100644 src/module3/2_2.py create mode 100644 src/module3/2_3.py create mode 100644 src/module3/2_4.py create mode 100644 src/module3/2_5.py create mode 100644 src/module3/2_6.py create mode 100644 src/module3/2_7.py create mode 100644 src/module3/3_1.py create mode 100644 src/module3/3_2.py create mode 100644 src/module3/3_3.py create mode 100644 src/module3/3_4.py create mode 100644 src/module3/4.py create mode 100644 src/module3/4_1.py create mode 100644 src/module3/4_2.py create mode 100644 src/module3/4_3.py create mode 100644 src/module3/4_4.py create mode 100644 src/module3/5_1.py create mode 100644 src/module3/5_2.py create mode 100644 src/module3/5_3.py create mode 100644 src/module3/5_4.py create mode 100644 src/module3/5_5.py create mode 100644 src/module3/5_6.py create mode 100644 src/module3/m3/1/py create mode 100644 src/module3/module3 diff --git a/src/bubble_sort.py b/src/bubble_sort.py new file mode 100644 index 0000000..e2ac83d --- /dev/null +++ b/src/bubble_sort.py @@ -0,0 +1,23 @@ +def summ(a, b): + return a + b + + + n = int(input()) + + arr = input() + str_lst = arr.split(" ") + res = [] + for item in str_lst: + res.append (int(item)) + #print(res) + n = len(res) + for i in range(0, n-1): + #print(f"i={i}") + for j in range(0, n-1-i): + #print (f"j={i}") + if res[j] > res[j+1]: + res[j] , res[j+1] = res[j+1], res[j] + print(" ".join(map(str,res))) + print (res) + + diff --git a/src/main10.py b/src/main10.py deleted file mode 100644 index 3b8c0f1..0000000 --- a/src/main10.py +++ /dev/null @@ -1,35 +0,0 @@ -def merge(Q, R): - i = j = 0 - inversions = 0 - res = [] - while i < len(Q) and j < len(R): - if Q[i] <= R[j]: - res.append(Q[i]) - i += 1 - else: - res.append(R[j]) - j += 1 - inversions += len(Q) - i - while i < len(Q): - res.append(Q[i]) - i += 1 - while j < len(R): - res.append(R[j]) - j += 1 - return res, inversions - -def merge_sort(B): - n = len(B) - if n <= 1: - return B, 0 - middle = n//2 - Q, l_inversions = merge_sort(B[0:middle]) - R, r_inversions = merge_sort(B[middle:n]) - res, inversions = merge(Q, R) - return res, inversions + l_inversions + r_inversions - -N = int(input()) -B = list(map(int, input().split(" "))) -B, total_inversions = merge_sort(B) -print(total_inversions) - diff --git a/src/main11.py b/src/main11.py deleted file mode 100644 index bda4050..0000000 --- a/src/main11.py +++ /dev/null @@ -1,33 +0,0 @@ -def merge(B, R, left_index, right_index): - i = j = 0 - res = [] - while i < len(B) and j < len(R): - if B[i] <= R[j]: - res.append(B[i]) - i += 1 - else: - res.append(R[j]) - j += 1 - while i < len(B): - res.append(B[i]) - i += 1 - while j < len(R): - res.append(R[j]) - j += 1 - print (f"{left_index} {right_index} {res[0]} {res[len(res) - 1]}") - return res - -def merge_sort(C, left_index, right_index): - n = len(C) - if n <= 1: - return C - middle = n//2 - B = merge_sort(C[0:middle], left_index, left_index+middle-1) - R = merge_sort(C[middle:n], left_index+middle, right_index) - - return merge(B, R, left_index, right_index) - -N = int(input()) -C = list(map(int, input().split(" "))) -c = merge_sort(C, 1, N) -print(" ".join(map(str,c))) diff --git a/src/main4.py/main5.py b/src/main4.py/main5.py deleted file mode 100644 index 3089e8f..0000000 --- a/src/main4.py/main5.py +++ /dev/null @@ -1,16 +0,0 @@ -N = input () -K = input () -q = k.split (" ") -a = [] -for la in q: - a.appended(int(ia)) -N = len(a) -t = 0 -for i in range(0, N-1): - for j in range(0, N-1-i): - if a[j]>a[j+1]: - a[j],a[j+1] = a[j+1],a[j] - e+=1 - print(" ",join(map(str, a))) -if t == 0: - print("0") diff --git a/src/main6.py b/src/main6.py deleted file mode 100644 index a24ec9f..0000000 --- a/src/main6.py +++ /dev/null @@ -1,8 +0,0 @@ -k = int(input()) -B = [0]*k -for i in range(k): - B[i] = list(map(int, input().split())) -B.sort(key=lambda x: x[0]) -B.sort(key=lambda x: x[1],reverse=1) -for i in range(k): - print(B[i][0], B[i][1]) \ No newline at end of file diff --git a/src/main7.py b/src/main7.py deleted file mode 100644 index 4fc2f86..0000000 --- a/src/main7.py +++ /dev/null @@ -1,7 +0,0 @@ -b = {} -s = int(input()) -n = input() -s = n.split() -for i in s: - b[int(i)] = 1 -print(len(b)) diff --git a/src/main8.py b/src/main8.py deleted file mode 100644 index 33252cc..0000000 --- a/src/main8.py +++ /dev/null @@ -1,11 +0,0 @@ -size1 = int(input()) -d = list(int(i) for i in input().split(" "))[:size1] -size2 = int(input()) -s = list(int(i) for i in input().split(" "))[:size2] -for i in range(0, size2): - d[s[i]-1] -= 1 -for i in range(0, size1): - if (d[i] < 0): - print("yes") - else: - print("no") \ No newline at end of file diff --git a/src/main9.py b/src/main9.py deleted file mode 100644 index e001b36..0000000 --- a/src/main9.py +++ /dev/null @@ -1,26 +0,0 @@ -def radix_sort(B, n): - digit = len(B[0]) - for i in range(digit): - buckets_arr = [[] for j in range(10)] - for item in B: - index = int(item)//10**i % 10 - buckets_arr[index].append(item) - print("Phase", i+1) - B=[] - for i in range(10): - print(f"Bucket {i}: ", end="") - print(", ".join(buckets_arr[i]) if len(buckets_arr[i])>0 else "empty") - B = B + buckets_arr[i] - print("**********") - return B - -N = int(input()) -B = [] -for i in range(N): - B.append(input()) -print("Initial array:") -print(", ".join(B)) -print("**********") -B = radix_sort(B, N) -print("Sorted array:") -print(", ".join(B)) diff --git a/src/module3/2_1.py b/src/module3/2_1.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_2.py b/src/module3/2_2.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_3.py b/src/module3/2_3.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_4.py b/src/module3/2_4.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_5.py b/src/module3/2_5.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_6.py b/src/module3/2_6.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/2_7.py b/src/module3/2_7.py new file mode 100644 index 0000000..e69de29 diff --git a/src/module3/3_1.py b/src/module3/3_1.py new file mode 100644 index 0000000..d0ce46e --- /dev/null +++ b/src/module3/3_1.py @@ -0,0 +1,28 @@ +def get_hash(s, k, p, d): + res = 0 + for j in range(0, k): + res=(res*d+ord(s[j]))%p + return res + + +def rabin_karp(s, q, p, d): + hq=get_hash(q, len(q), p, d) + hs=get_hash(s, len(q), p, d) + dq=1 + l=[] + for j in range (len(q)): + dq=(dq*d)%p + for j in range(len(s)-len(q)+1): + if hq==hs: + l.append(j) + if j+len(q) 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + k[i + 1] = 0 + return k +#префикс функция + +A = input() +n=len(A) +res = pref_funct(A,n) +k = n - res[n-1] +if (n % k == 0): + print(n//k) +else: + print(1) \ No newline at end of file diff --git a/src/module3/3_4.py b/src/module3/3_4.py new file mode 100644 index 0000000..618067f --- /dev/null +++ b/src/module3/3_4.py @@ -0,0 +1,18 @@ +def pref_funct(s,n): + k = [0] * n + k[0] = 0 + for i in range (n - 1): + j = k[i] + while (j > 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + K[i + 1] = 0 + return k +#префикс функция + +A = input() +n = len(A) +pref = pref_funct(A,n) +print(n - pref[-1]) \ No newline at end of file diff --git a/src/module3/4.py b/src/module3/4.py new file mode 100644 index 0000000..14a6630 --- /dev/null +++ b/src/module3/4.py @@ -0,0 +1,20 @@ +def pref_funct(s): + n = len(s) + k = [0] * n + k[0] = 0 + for i in range (n - 1): + j = k[i] + while (j > 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + k[i + 1] = 0 + return k +#префикс функция + +text = str(input()) +s = list(text) +n = len(s) +pref = pref_funct(s) +print(n - pref[-1]) \ No newline at end of file diff --git a/src/module3/4_1.py b/src/module3/4_1.py new file mode 100644 index 0000000..df97194 --- /dev/null +++ b/src/module3/4_1.py @@ -0,0 +1,14 @@ +def funct(s): + k = 0 + l = [] + for i in s: + if (i == '('): + l.append(i) + elif (l != []) and (l[-1] == '('): + l.pop() + else: + k += 1 + return k+len(l) + +s = input() +print(funct(s)) \ No newline at end of file diff --git a/src/module3/4_2.py b/src/module3/4_2.py new file mode 100644 index 0000000..f468c10 --- /dev/null +++ b/src/module3/4_2.py @@ -0,0 +1,23 @@ +def poezd_sort(A, n): + stack = [] + B = [] + poezd_sorted = sorted(A) + while(len(B) != n): + if (stack == []): + stack.append(A[0]) + A.pop(0) + continue + if (len(A) > 0) and (A[0] <= stack[-1]): + stack.append(A[0]) + A.pop(0) + else: + B.append(stack[-1]) + stack.pop() + return B == poezd_sorted + +N = int(input()) +poezd = list(map(int, input().split())) +if poezd_sort(poezd, N): + print("YES") +else: + print("NO") \ No newline at end of file diff --git a/src/module3/4_3.py b/src/module3/4_3.py new file mode 100644 index 0000000..14381d0 --- /dev/null +++ b/src/module3/4_3.py @@ -0,0 +1,23 @@ +def poisk(A, n): + stack = [n-1] + a = [0]*n #a-индексы + a[n-1] = -1 + for i in range(n-2, -1, -1): + if A[stack[-1]] >= A[i]: + while (stack != []) and (A[stack[-1]] >= A[i]): + stack.pop() + if (stack == []): + a[i] = -1 + else: + a[i] = stack[-1] + stack.append(i) + else: + a[i] = stack[-1] + stack.append(i) + return a + + +N = int(input()) +A = list(map(int, input().split())) +A = poisk(A, N) +print(' '.join(map(str, A))) \ No newline at end of file diff --git a/src/module3/4_4.py b/src/module3/4_4.py new file mode 100644 index 0000000..7892308 --- /dev/null +++ b/src/module3/4_4.py @@ -0,0 +1,18 @@ +def min_otrezok(A, n, k): + stack = [] + for i in range(k): + while (stack != []) and (A[i] <= A[stack[-1]]): + stack.pop() + stack.append(i) + for i in range(k, n): + print(A[stack[0]]) + while (stack != []) and (i-k >= stack[0]): + stack.pop(0) + while (stack != []) and (A[i] <= A[stack[-1]]): + stack.pop() + stack.append(i) + print(A[stack[0]]) + +N, K = map(int, input().split()) +A = list(map(int, input().split())) +min_otrezok(A, N, K) \ No newline at end of file diff --git a/src/module3/5_1.py b/src/module3/5_1.py new file mode 100644 index 0000000..6e2b5b7 --- /dev/null +++ b/src/module3/5_1.py @@ -0,0 +1,55 @@ +class Node: + __size = 0 + __height = 0 + + def __init__(self, data): + self.data = data + self.left = None + self.right = None + Node.__size += 1 + + @property + def size(self): + return self.__size + + @property + def height(self): + return self.__height + + def add(self, value, depth): + if self.data == value: + return + if value < self.data: + if self.left: + self.left.add(value, depth+1) + else: + if Node.__height < depth: + Node.__height = depth + self.left = Node(value) + else: + if self.right: + self.right.add(value, depth+1) + else: + if Node.__height < depth: + Node.__height = depth + self.right = Node(value) + + def find_forks(self): + if self.left: + self.left.find_forks() + if self.left and not self.right: + print(self.data) + if self.right and not self.left: + print(self.data) + if self.right: + self.right.find_forks() + +def build_tree(elmnt): + tree = Node(elmnt[0]) + for i in range(1, len(elmnt)-1): + tree.add(elmnt[i], 1) + return tree + +lisst = list(map(int, input().split())) +tree = build_tree(lisst) +tree.find_forks() \ No newline at end of file diff --git a/src/module3/5_2.py b/src/module3/5_2.py new file mode 100644 index 0000000..2b379e8 --- /dev/null +++ b/src/module3/5_2.py @@ -0,0 +1,47 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + + def add(self, data): + if data == self.data: + return + if data < self.data: + if self.left: + self.left.add(data) + else: + self.left = Node(data) + else: + if self.right: + self.right.add(data) + else: + self.right = Node(data) + +def height(tree): + if tree is None: + return 0 + return max(height(tree.left), height(tree.right))+1 + +def build_tree(elements): + root = Node(elements[0]) + for i in range(1, len(elements)): + root.add(elements[i]) + return root + +def balance(tree): + if not tree or ((height(tree.left) == height(tree.right) or height(tree.left)+1 == height(tree.right) or height(tree.left) == height(tree.right)+1) and balance(tree.right) and balance(tree.left)): + return True + return False + +def main(): + n = [int(i) for i in input().split()] + n.pop() + tree = build_tree(n) + + if balance(tree): + print("YES") + else: + print("NO") + +main() \ No newline at end of file diff --git a/src/module3/5_3.py b/src/module3/5_3.py new file mode 100644 index 0000000..537dd7a --- /dev/null +++ b/src/module3/5_3.py @@ -0,0 +1,39 @@ +def build(v, l, r, segment_tree, nums): + if r - l == 1: + segment_tree[v] = nums[l] + return + m = (r + l) // 2 + build(2 * v + 1, l, m, segment_tree, nums) + build(2 * v + 2, m, r, segment_tree, nums) + segment_tree[v] = nod(segment_tree[2 * v + 1], segment_tree[2 * v + 2]) + +def nod(a,b): + while b: + a, b = b, a%b + return a + +def getNOD(v, l, r, segment_tree, ql, qr): + if ql <= l and qr >= r: + return segment_tree[v] + if ql >= r or qr <= l: + return 0 + m = (r + l) // 2 + st_l = getNOD(2 * v + 1, l, m, segment_tree, ql, qr) + st_r = getNOD(2 * v + 2, m, r, segment_tree, ql, qr) + return nod(st_l, st_r) + +def main(): + n = int(input()) + numbers = list(map(int, input().split()))[:n] + segment_tree = [0] * 4 * n + build(0, 0, n, segment_tree, numbers) + q = int(input()) + a = [] + + while q != 0: + l, r = map(int, input().split()) + a.append(getNOD(0, 0, n, segment_tree, l - 1, r)) + q -= 1 + print(*a) + +main() \ No newline at end of file diff --git a/src/module3/5_4.py b/src/module3/5_4.py new file mode 100644 index 0000000..ed61ba0 --- /dev/null +++ b/src/module3/5_4.py @@ -0,0 +1,48 @@ +def build(v, l, r, seg_tree, a): + if r-l == 1: + seg_tree[v] = a[l] + return + m = (r+l)//2 + build(2*v+1, l, m, seg_tree, a) + build(2*v+2, m, r, seg_tree, a) + seg_tree[v] = seg_tree[2*v+1] + seg_tree[2*v+2] + +def search_k0(v, l, r, seg_tree, k): + if k > seg_tree[v]: + return -1 + if r - l == 1: + return r + m = (r+l)//2 + if seg_tree[2*v+1] >= k: + return search_k0(2*v+1, l, m, seg_tree, k) + else: + return search_k0(2*v+2, m, r, seg_tree, k - seg_tree[2*v+1]) + +def get_sum(v, l, r, seg_tree, ql, qr): + if ql <= l and qr >= r: + return seg_tree[v] + if ql >= r or qr <= l: + return 0 + m = (r+l)//2 + s_tl = get_sum(2*v+1, l, m, seg_tree, ql, qr) + s_tr = get_sum(2*v+2, m, r, seg_tree, ql, qr) + return s_tl + s_tr +def main(): + num = int(input()) + a = [0 if int(i) != 0 else 1 for i in input().split()] + seg_tree = [0]*4*num + build(0, 0, num, seg_tree, a) + q = int(input()) + ind = [] + while q != 0: + l, r, k = map(int, input().split()) + sum = get_sum(0, 0, num, seg_tree, l-1, r) + if sum >= k and l > 1: + ind.append(search_k0(0, 0, num, seg_tree, k+get_sum(0, 0, num, seg_tree, 0, l-1))) + elif sum >= k and l == 1: + ind.append(search_k0(0, 0, num, seg_tree, k)) + else: + ind.append(-1) + q -= 1 + print(*ind) +main() \ No newline at end of file diff --git a/src/module3/5_5.py b/src/module3/5_5.py new file mode 100644 index 0000000..bdee6f0 --- /dev/null +++ b/src/module3/5_5.py @@ -0,0 +1,48 @@ +from math import gcd + +def build(v, l, r, seg_tree, a): + if r-l == 1: + seg_tree[v] = a[l] + return + m = (l+r)//2 + build(2*v + 1, l, m, seg_tree, a) + build(2*v + 2, m, r, seg_tree, a) + seg_tree[v] = gcd(seg_tree[2*v + 1], seg_tree[2*v + 2]) + +def get_gcd(v, l, r, seg_tree, ql, qr): + if ql <= l and qr >= r: + return seg_tree[v] + if ql >= r or qr <= l: + return 0 + m = (r+l)//2 + s_tl = get_gcd(2*v+1, l, m, seg_tree, ql, qr) + s_tr = get_gcd(2*v+2, m, r, seg_tree, ql, qr) + return gcd(s_tl, s_tr) + +def update(v, l, r, seg_tree, idx, value): + if r-l == 1: + seg_tree[v] = value + return + m = (r+l)//2 + if idx < m: + update(2*v+1, l, m, seg_tree, idx, value) + else: + update(2*v+2, m, r, seg_tree, idx, value) + seg_tree[v] = gcd(seg_tree[2*v + 1], seg_tree[2*v + 2]) + +def main(): + num = int(input()) + seg_tree = [0]*4*num + a = list(map(int, input().split()))[:num] + build(0, 0, num, seg_tree, a) + q = int(input()) + res = [] + while q != 0: + que, l, r = map(str, input().split()) + if que=="s": + res.append(get_gcd(0, 0, num, seg_tree, int(l)-1, int(r))) + else: + update(0, 0, num, seg_tree, int(l)-1, int(r)) + q -= 1 + print(*res) +main() \ No newline at end of file diff --git a/src/module3/5_6.py b/src/module3/5_6.py new file mode 100644 index 0000000..ade3ed4 --- /dev/null +++ b/src/module3/5_6.py @@ -0,0 +1,67 @@ +def build(v, l, r, seg_tree, a): + if r-l == 1: + seg_tree[v] = a[l] + return + m = (r+l)//2 + build(2*v+1, l, m, seg_tree, a) + build(2*v+2, m, r, seg_tree, a) + seg_tree[v] = seg_tree[2*v+1] + seg_tree[2*v+2] + +def search_k0(v, l, r, seg_tree, k): + if k > seg_tree[v]: + return -1 + if r - l == 1: + return r + m = (r+l)//2 + if seg_tree[2*v+1] >= k: + return search_k0(2*v+1, l, m, seg_tree, k) + else: + return search_k0(2*v+2, m, r, seg_tree, k - seg_tree[2*v+1]) + +def get_sum(v, l, r, seg_tree, ql, qr): + if ql <= l and qr >= r: + return seg_tree[v] + if ql >= r or qr <= l: + return 0 + m = (r+l)//2 + tl = get_sum(2*v+1, l, m, seg_tree, ql, qr) + tr = get_sum(2*v+2, m, r, seg_tree, ql, qr) + return tl + tr + +def update(v, l, r, seg_tree, ind_new, value): + if r-l == 1: + seg_tree[v] = value + return + m = (r+l)//2 + if ind_new < m: + update(2*v+1, l, m, seg_tree, ind_new, value) + else: + update(2*v+2, m, r, seg_tree, ind_new, value) + seg_tree[v] = seg_tree[2*v+1] + seg_tree[2*v+2] +def main(): + n = int(input()) + a = [0 if int(i) != 0 else 1 for i in input().split()] + seg_tree = [0]*4*n + build(0, 0, n, seg_tree, a) + q = int(input()) + ind = [] + while q != 0: + i = input().split() + if len(i) == 4: + l, r, k = int(i[1]), int(i[2]), int(i[3]) + sum_ = get_sum(0, 0, n, seg_tree, l-1, r) + if sum_ >= k and l > 1: + ind.append(search_k0(0, 0, n, seg_tree, k+get_sum(0, 0, n, seg_tree, 0, l-1))) + elif sum_ >= k and l == 1: + ind.append(search_k0(0, 0, n, seg_tree, k)) + else: + ind.append(-1) + else: + i, v = int(i[1]), int(i[2]) + if v == 0: + update(0, 0, n, seg_tree, i-1, 1) + else: + update(0, 0, n, seg_tree, i-1, 0) + q -= 1 + print(*ind) +main() \ No newline at end of file diff --git a/src/module3/m3/1/py b/src/module3/m3/1/py new file mode 100644 index 0000000..9162468 --- /dev/null +++ b/src/module3/m3/1/py @@ -0,0 +1,14 @@ +def funct(s): + k = 0 + l = [] + for i in s: + if (i == '('): + l.append(i) + elif (l != []) and (l[-1] == '('): + l.pop() + else: + k += 1 + return k+len(l) + +s = str(input()) +print(funct(s)) \ No newline at end of file diff --git a/src/module3/module3 b/src/module3/module3 new file mode 100644 index 0000000..b613fb0 --- /dev/null +++ b/src/module3/module3 @@ -0,0 +1,26 @@ +def pref_funct(s): + n = len(s) + k = [0] * n + k[0] = 0 + for i in range (n - 1): + j = k[i] + while (j > 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + k[i + 1] = 0 + return k +#префикс функция + +text = str(input()) +s = list(text) +pref = pref_funct(s) +n=len(s) +for i in range(n - 1, n): + res = n - pref[i] + +if (n % res == 0): + print(n // res) +else: + print('1') \ No newline at end of file