优秀代码样板收集计划(python)

Posted xlinsist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了优秀代码样板收集计划(python)相关的知识,希望对你有一定的参考价值。

  • dfs,thread,python3,defaultdict,换根dp
  • Counter,元组map
  • 捕获异常
  • 数组排序翻转切片
  • bfs
  • accumulate
  • 二维数组读入,math库调用
  • 字符串
  • counter数组
  • bisect(lower_bound upper_bound)
  • 列表推导式

dfs,thread,python3,defaultdict,换根dp

from collections import defaultdict
import threading
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
threading.stack_size(10**8)
‘‘‘
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for CASES in range(int(input())):
‘‘‘
inf = 100000000000000000  # 1e17
mod = 998244353

A = defaultdict(list)
son = defaultdict(int)
dp = defaultdict(int)

n = int(input())
for CASES in range(n - 1):
    x, y = map(int, input().split())
    A[x].append(y)
    A[y].append(x)


def dfs1(now, fa):  # get son
    son[now] = 1
    for to in A[now]:
        if to == fa:
            continue
        dfs1(to, now)
        son[now] += son[to]


def dfs2(now, fa):  # get dp
    dp[now] = son[now]
    for to in A[now]:
        if to == fa:
            continue
        dfs2(to, now)
        dp[now] += dp[to]


def dfs3(now, fa):  # transfer
    global ans
    ans = max(ans, dp[now])
    for to in A[now]:
        if to == fa:
            continue
        dp[now] -= dp[to] + son[to]
        dp[to] += dp[now] + (n - son[to])
        dfs3(to, now)
        dp[to] -= dp[now] + (n - son[to])
        dp[now] += dp[to] + son[to]


def main():
    dfs1(1, 0)
    dfs2(1, 0)
    dfs3(1, 0)


ans = 0
t = threading.Thread(target=main)
t.start()
t.join()
print(ans)
from collections import Counter
import sys
input = sys.stdin.readline
‘‘‘
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for CASES in range(int(input())):
‘‘‘
inf = 100000
mod = 998244353


def fac(x):
    i = 2
    while i * i <= x:
        if x % i == 0:
            cnt = 0
            while x % i == 0:
                cnt += 1
                x //= i
            cnt %= k
            if cnt > 0:
                sig.append((i, cnt))
        i += 1
    if x > 1:
        sig.append((x, 1))


n, k = map(int, input().split())
A = list(map(int, input().split()))

ans = 0
prev = Counter()
for a in A:
    sig = []
    fac(a)
    com_sig = []
    for p, val in sig:
        com_sig.append((p, k - val))
    ans += prev[tuple(sig)]
    prev[tuple(com_sig)] += 1
print(ans)

    try:
        n = int(v)
    except Exception as e:
        print("Couldn‘t parse")
        print(‘Reason:‘, e)

数组

import sys
input = sys.stdin.readline
‘‘‘
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
for test in range(int(input())):
‘‘‘
inf = 100000000000000000  # 1e17

x, y, a, b, c = map(int, input().split())
array_a = list(map(int, input().split()))
array_b = list(map(int, input().split()))
array_c = list(map(int, input().split()))
array_a = sorted(array_a, reverse=True)[:x]
array_b = sorted(array_b, reverse=True)[:y]
ans = sum(sorted(array_a + array_b + array_c, reverse=True)[:x + y])
print(ans)

bfs

from itertools import accumulate
from collections import deque
def bfs(STA):
    DIS = [-1] * (n + 1)
    Q = deque([STA])
    DIS[STA] = 0
    while Q:
        x = Q.pop()
        for to in A[x]:
            if DIS[to] == -1:
                DIS[to] = DIS[x] + 1
                Q.appendleft(to)
    return DIS

accumulate

input = sys.stdin.readline
from itertools import accumulate
 
t=int(input())
for tests in range(t):
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    ANS=[0]*(k*2+3)
 
    for i in range(n//2):
        x,y=A[i],A[n-1-i]
        if x>y:
            x,y=y,x
 
        #print(x,y)
 
        ANS[x+1]-=1
        ANS[y+k+1]+=1
        ANS[x+y]-=1
        ANS[x+y+1]+=1
 
        #print(ANS)
 
    #print(ANS)
 
    S=list(accumulate(ANS))
    #print(S)
 
    print(n+min(S))

二维数组读入,math库调用

# https://codeforces.com/contest/1220/problem/B
import sys
input=sys.stdin.readline

n=int(input())
A=[list(map(int,input().split())) for i in range(n)]

from math import ceil,sqrt
ANS=[]
tmp=A[0][1]*A[0][2]//A[1][2]
ANS.append(ceil(sqrt(tmp)))
for i in range(1,n):
    ANS.append(A[0][i]//ANS[0])
print(*ANS)

字符串

import sys
input=sys.stdin.readline

n=input()
S=input().strip()

one=S.count("n")
zero=S.count("z")
ANS=[1]*one+[0]*zero
print(*ANS)

Counter数组

https://codeforces.com/contest/1208/problem/B
import sys
input=sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))

from collections import Counter
import copy
C=Counter(A)
for a in A:
    if C[a]<=1:
        del C[a]
if not(C):
    print(0)
    sys.exit()

ans=n-1
for i in range(n):
    D=copy.deepcopy(C)
    for j in range(i,n):
        if D[A[j]]>1:
            D[A[j]]-=1
        if D[A[j]]==1:
            del D[A[j]]
        if not(D):
            ans=min(ans,j-i+1)
            break
print(ans)

bisect

import bisect

L = [1,3,3,3,6,8,12,15]
x = 3

x_insert_point = bisect.bisect_left(L,x)
print (x_insert_point)

x_insert_point = bisect.bisect_right(L,x)
print(x_insert_point)

列表推导式

# https://codeforces.com/contest/1332/problem/C
def f(l):
    d={}
    for i in l:
        if i in d:
            d[i]+=1
        else:
            d[i]=1
    return len(l)-max(d.values())

import sys
for _ in range(int(sys.stdin.readline())):
    n,k=map(int,sys.stdin.readline().split())
    s=input()
    print(sum(f(s[j::k]+s[k-j-1::k]) for j in range(k//2))+sum(f(s[k//2::k]) for j in range(k%2)))

进制转换

def toshi(x):
    if ‘0‘<=x<=‘9‘:
        return ord(x)-48
    return ord(x)-ord(‘a‘)+10
def shito(x):
    if 0<=x<=9:
        return chr(x+48)
    return chr(x-10+ord(‘a‘))
s=input()
a,b=map(int,input().split())
ans=0
for i in s:
    ans*=a
    ans+=toshi(i)
if ans==0:
    print(0)
pr=‘‘
while ans>0:
    pr=shito(ans%b)+pr
    ans//=b
print(pr)

以上是关于优秀代码样板收集计划(python)的主要内容,如果未能解决你的问题,请参考以下文章

Word 文档的优秀代码片段工具或插件?

massCode 一款优秀的开源代码片段管理器

Microsoft SQL Server 代码片段收集

精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解

精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!(转载)

使用 Apollo 客户端的片段组合:约定和样板