From 6b40f708b277a10b5883fa1085d679d2863ff653 Mon Sep 17 00:00:00 2001 From: licsber Date: Fri, 28 Oct 2022 02:37:25 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E7=94=B5=E4=BF=A1=E7=A0=94=E5=8F=91?= =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E5=B8=88=E8=80=83=E8=AF=95=E5=86=85=E5=AE=B9?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/Algorithm.iml | 6 ++- .../inspectionProfiles/profiles_settings.xml | 1 + .idea/misc.xml | 2 +- ct/__init__.py | 0 ct/l1/8-平衡数.py | 17 +++++++ ct/l2/14-Ruby和薯条.py | 15 +++++++ ct/l2/15-小红无敌.py | 19 ++++++++ ct/l2/16-优美字符串.py | 22 ++++++++++ ct/l2/17-重排字符串.py | 33 ++++++++++++++ ct/l2/18-指定值倍数取数.py | 22 ++++++++++ ct/l2/19-寻找公倍数和公约数.py | 16 +++++++ ct/l2/20-Ranko的手表.py | 27 ++++++++++++ ct/l2/3-动态规划-不相邻取数.py | 13 ++++++ ct/l2/__init__.py | 0 ct/l2/两数乘积k.py | 13 ++++++ ct/l2/回文.py | 12 +++++ ct/l2/打家劫舍3.py | 44 +++++++++++++++++++ ct/main.py | 0 luogu/P1216/main.py | 21 +++++++++ luogu/__init__.py | 0 main.py | 9 ++++ 21 files changed, 289 insertions(+), 3 deletions(-) create mode 100644 ct/__init__.py create mode 100644 ct/l1/8-平衡数.py create mode 100644 ct/l2/14-Ruby和薯条.py create mode 100644 ct/l2/15-小红无敌.py create mode 100644 ct/l2/16-优美字符串.py create mode 100644 ct/l2/17-重排字符串.py create mode 100644 ct/l2/18-指定值倍数取数.py create mode 100644 ct/l2/19-寻找公倍数和公约数.py create mode 100644 ct/l2/20-Ranko的手表.py create mode 100644 ct/l2/3-动态规划-不相邻取数.py create mode 100644 ct/l2/__init__.py create mode 100644 ct/l2/两数乘积k.py create mode 100644 ct/l2/回文.py create mode 100644 ct/l2/打家劫舍3.py create mode 100644 ct/main.py create mode 100644 luogu/P1216/main.py create mode 100644 luogu/__init__.py create mode 100644 main.py diff --git a/.idea/Algorithm.iml b/.idea/Algorithm.iml index d0876a7..e91ef14 100644 --- a/.idea/Algorithm.iml +++ b/.idea/Algorithm.iml @@ -1,8 +1,10 @@ - - + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml index 105ce2d..dd4c951 100644 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -1,5 +1,6 @@ + diff --git a/.idea/misc.xml b/.idea/misc.xml index d56657a..a1bbe46 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/ct/__init__.py b/ct/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ct/l1/8-平衡数.py b/ct/l1/8-平衡数.py new file mode 100644 index 0000000..457d448 --- /dev/null +++ b/ct/l1/8-平衡数.py @@ -0,0 +1,17 @@ +def f(x): + y = 1 + for j in x: + y *= int(j) + return y + + +n = input() +flag = True +for i in range(1, len(n)): + left, right = n[:i], n[i:] + if f(left) == f(right): + flag = False + print("YES") + break +if flag: + print("No") diff --git a/ct/l2/14-Ruby和薯条.py b/ct/l2/14-Ruby和薯条.py new file mode 100644 index 0000000..047e5ba --- /dev/null +++ b/ct/l2/14-Ruby和薯条.py @@ -0,0 +1,15 @@ +import io +import sys + +sys.stdin = io.StringIO('''5 2 3 +3 1 6 2 5''') # 4 + +n, l, r = map(int, input().split()) +a = list(map(int, input().split())) +s = 0 +for i in range(n - 1): + for j in range(i + 1, n): + if l <= abs(a[i] - a[j]) <= r: + s += 1 + +print(s) diff --git a/ct/l2/15-小红无敌.py b/ct/l2/15-小红无敌.py new file mode 100644 index 0000000..39828a9 --- /dev/null +++ b/ct/l2/15-小红无敌.py @@ -0,0 +1,19 @@ +import io +import sys + +sys.stdin = io.StringIO('''2 3 1 3''') # 26 + +a, h, b, k = map(int, input().split()) +c = 0 +while h > 0 and k > 0: + c += a + c += b + h -= b + k -= a + +if h > 0 > k: + c += a * 10 +elif h <= 0 < k: + c += b * 10 + +print(c) diff --git a/ct/l2/16-优美字符串.py b/ct/l2/16-优美字符串.py new file mode 100644 index 0000000..0accc36 --- /dev/null +++ b/ct/l2/16-优美字符串.py @@ -0,0 +1,22 @@ +import io +import sys + +sys.stdin = io.StringIO('''4 +a +ab +abbc +aaabb''') # 1 2 5 8 + +t = int(input()) +s = [] +for _ in range(t): + s.append(input()) + +for i in s: + k = 0 + for j in range(len(i)): + if j + 1 < len(i): + if i[j] == i[j + 1]: + k += 1 + + print(k + len(i)) diff --git a/ct/l2/17-重排字符串.py b/ct/l2/17-重排字符串.py new file mode 100644 index 0000000..cb934fd --- /dev/null +++ b/ct/l2/17-重排字符串.py @@ -0,0 +1,33 @@ +import io +import sys + +sys.stdin = io.StringIO('''1 +aabbccc''') # cabcacb + +from collections import Counter + + +def reorganizeString(str): + cnt = Counter(str) + # 桶的数目等于字符串中最多的元素的数目 + bucketNum = cnt.most_common(1)[0][1] + buckets = [[] for _ in range(bucketNum)] + idx = 0 + # 优先填充数目最多的元素 + for c, num in cnt.most_common(): + while num: + buckets[idx].append(c) + # 循环在不同桶中进行填充 + idx = (idx + 1) % bucketNum + num -= 1 + return "".join(["".join(bucket) for bucket in buckets]) if list(map(len, buckets)).count(1) <= 1 else "" + + +n = int(input()) +str = input() +result = reorganizeString(str) +if result == '': + print('no') +else: + print('yes') + print(result) diff --git a/ct/l2/18-指定值倍数取数.py b/ct/l2/18-指定值倍数取数.py new file mode 100644 index 0000000..99a7625 --- /dev/null +++ b/ct/l2/18-指定值倍数取数.py @@ -0,0 +1,22 @@ +import io +import sys + +sys.stdin = io.StringIO('''5 5 +4 8 2 9 1''') # 20 + +n, k = map(int, input().split()) +a = list(map(int, input().split())) +dp = [[-1] * k for i in range(n + 1)] +dp[0][0] = 0 +for i in range(n + 1): + for j in range(k): + if dp[i - 1][j] != -1: + dp[i][j] = dp[i - 1][j] + tmp = (j - a[i - 1] % k + k) % k + if dp[i - 1][tmp] != -1: + dp[i][j] = max(dp[i][j], dp[i - 1][tmp] + a[i - 1]) + +if dp[n][0] == 0: + print(-1) +else: + print(dp[n][0]) diff --git a/ct/l2/19-寻找公倍数和公约数.py b/ct/l2/19-寻找公倍数和公约数.py new file mode 100644 index 0000000..2497fe1 --- /dev/null +++ b/ct/l2/19-寻找公倍数和公约数.py @@ -0,0 +1,16 @@ +import io +import sys + +sys.stdin = io.StringIO('''2 60 3 96''') # 12 / 6 + +a = list(map(int, input().split())) +_min, _max = min(a), max(a) +k = 0 +for i in range(_min, _max): + if i % a[0] + i % a[2] + a[1] % i + a[3] % i == 0: + k += 1 + print(i) + break + +if k == 0: + print(-1) diff --git a/ct/l2/20-Ranko的手表.py b/ct/l2/20-Ranko的手表.py new file mode 100644 index 0000000..e02a9e5 --- /dev/null +++ b/ct/l2/20-Ranko的手表.py @@ -0,0 +1,27 @@ +import io +import sys + +sys.stdin = io.StringIO('''18:0? +2?:1?''') # 121 319 + + +def check(tStr, tInt): + s = '%02d' % (tInt / 60) + ':' + '%02d' % (tInt % 60) + for x in range(len(s)): + if tStr[x] != '?' and tStr[x] != s[x]: + return False + + return True + + +tStr1 = input() +tStr2 = input() +minT, maxT = 9999, 0 +for i in range(0, 24 * 60): + if check(tStr1, i): + for j in range(i + 1, 24 * 60): + if check(tStr2, j): + minT = j - i if 0 < j - i < minT else minT + maxT = j - i if maxT < j - i else maxT + +print(minT, maxT) diff --git a/ct/l2/3-动态规划-不相邻取数.py b/ct/l2/3-动态规划-不相邻取数.py new file mode 100644 index 0000000..66e0ed1 --- /dev/null +++ b/ct/l2/3-动态规划-不相邻取数.py @@ -0,0 +1,13 @@ +import io +import sys + +sys.stdin = io.StringIO('''4 +2 6 4 1''') # 7 + +n = int(input()) +a = list(map(int, input().split())) +dp0, dp1 = 0, a[0] +for i in range(1, n): + dp0, dp1 = dp1, max(dp1, dp0 + a[i]) + +print(dp1) diff --git a/ct/l2/__init__.py b/ct/l2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ct/l2/两数乘积k.py b/ct/l2/两数乘积k.py new file mode 100644 index 0000000..423ff11 --- /dev/null +++ b/ct/l2/两数乘积k.py @@ -0,0 +1,13 @@ +from bisect import * + +n, k = map(int, input().split()) +a = sorted(list(map(int, input().split()))) +pair = [0, 0, 0] +for idx in range(n - 1): + l = bisect_left(a, k / a[idx], idx + 1, n) + r = bisect_right(a, k / a[idx], idx + 1, n) + pair[0] += n - r + pair[1] += r - l + pair[2] += l - idx - 1 + +print(*pair) diff --git a/ct/l2/回文.py b/ct/l2/回文.py new file mode 100644 index 0000000..9fbdff5 --- /dev/null +++ b/ct/l2/回文.py @@ -0,0 +1,12 @@ +def solution(s): + length = len(s) + for n in range(2, length + 1): + for i in range(0, length - n + 1): + if s[i:i + n - 1] == s[i + n - 1:i:-1]: + print(n) + return + print("-1") + + +s = input() +solution(s) diff --git a/ct/l2/打家劫舍3.py b/ct/l2/打家劫舍3.py new file mode 100644 index 0000000..d134ff9 --- /dev/null +++ b/ct/l2/打家劫舍3.py @@ -0,0 +1,44 @@ +import sys + +sys.setrecursionlimit(100010) # 修改默认递归深度 + + +class BiTNode(object): # 二叉树节点 + def __init__(self, data): + self.data = data + self.dp = [0, 0] # dp[0]表示不偷该节点,dp[1]表示偷该节点 + self.lChild = None + self.rChild = None + + +# 二叉树的后序遍历 +def dfs(T): + if T != None: + dfs(T.lChild) + dfs(T.rChild) + lChildDp = [0, 0] + rChildDp = [0, 0] + if T.lChild != None: + lChildDp = T.lChild.dp + if T.rChild != None: + rChildDp = T.rChild.dp + # 不偷该节点,左节点的最优情况(偷与不偷取大者) + 右节点的最优情况(偷与不偷取大者) + T.dp[0] = max(lChildDp[0], lChildDp[1]) + max(rChildDp[0], rChildDp[1]) + # 偷该节点,该节点的现金 + 不偷左节点获得的现金 + 不偷右节点获得的现金 + T.dp[1] = T.data + lChildDp[0] + rChildDp[0] + + +n = int(input()) +nodes = list(map(int, input().split())) +fathers = list(map(int, input().split())) +nodeArray = [BiTNode(None) for i in range(n)] +for i in range(n): # 构造二叉树 + nodeArray[i].data = nodes[i] + f = fathers[i] - 1 # 节点编号从0开始 + if f >= 0: + if nodeArray[f].lChild == None: + nodeArray[f].lChild = nodeArray[i] + else: + nodeArray[f].rChild = nodeArray[i] +dfs(nodeArray[0]) # 二叉树的后序遍历,nodeArray[0]为根节点 +print(max(nodeArray[0].dp[0], nodeArray[0].dp[1])) diff --git a/ct/main.py b/ct/main.py new file mode 100644 index 0000000..e69de29 diff --git a/luogu/P1216/main.py b/luogu/P1216/main.py new file mode 100644 index 0000000..c3d3f41 --- /dev/null +++ b/luogu/P1216/main.py @@ -0,0 +1,21 @@ +import io +import sys + +sys.stdin = io.StringIO('''5 +7 +3 8 +8 1 0 +2 7 4 4 +4 5 2 6 5''') + +n = int(input()) +nums = [] +for i in range(n): + nums.append(list(map(int, input().split()))) + +f = [[0] * (n + 1) for _ in range(n + 1)] +for i in range(n - 1, -1, -1): + for j in range(i + 1): + f[i][j] = max(f[i + 1][j], f[i + 1][j + 1]) + nums[i][j] + +print(f[0][0]) diff --git a/luogu/__init__.py b/luogu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..256c2bc --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +import io +import sys + +sys.stdin = io.StringIO('''5 +7 +3 8 +8 1 0 +2 7 4 4 +4 5 2 6 5''')