首页 > 编程语言 > 详细

优秀代码收集计划(python)

时间:2020-04-17 23:30:05      阅读:75      评论:0      收藏:0      [点我收藏+]

counter数组

bisect(lower_bound upper_bound)

列表推导式

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)

原文:https://www.cnblogs.com/reshuffle/p/12242314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!