头像

打十铜人ing




离线:5小时前


最近来访(87)
用户头像
阿尔托莉雅_saber
用户头像
JimmyHu
用户头像
刷题家族组长-省赛加油
用户头像
用户头像
琉璃_1
用户头像
希尔维亚
用户头像
山东交通大学
用户头像
niumaa
用户头像
你回来了
用户头像
有点大的青椒
用户头像
此ID已被其他用户占用
用户头像
不拿周赛牌不改名
用户头像
Mr.Lonely
用户头像
R星_魔鬼训练ing
用户头像
RandyLuo
用户头像
Ethereum
用户头像
Tequila
用户头像
阿蒙
用户头像
zdkk
用户头像
abcdeab

活动打卡代码 AcWing 1504. 火星颜色

lst=list(map(int,input().split()))
dic={i:str(i) for i in range(10)}
dic[10]="A"
dic[11]="B"
dic[12]="C"
def get(x):
    if x<13:
        res="0"+dic[x]
    else:
        res=dic[x//13]+dic[x%13]
    return res
ans="#"
for i in lst:
    ans+=get(i)
print(ans)



活动打卡代码 AcWing 1492. 可逆质数

def is_prime(x):
    if x==1:
        return False
    i=2
    while i*i<=x:
        if x%i==0:
            return False
        i+=1
    return True
def rev(x,d):
    ans=0
    while x:
        ans=ans*d+x%d
        x//=d
    return ans
while True:
    try:
        a,b=map(int,input().split())
        if is_prime(a)  and is_prime(rev(a,b)):
            print("Yes")
        else:
            print("No")
    except:
        break


活动打卡代码 AcWing 1482. 进制

a,b,tag,radix=input().split()
mapping = {str(i): i for i in range(10)}
mapping.update({chr(i): i-ord("a")+10 for i in range(ord("a"), ord("z")+1)})

if tag=='2':
    a,b=b,a
def get(n,r):
    ans=0
    idx=1
    for i in range(1,len(n)+1):
        ans+=mapping[n[-i]]*idx
        idx*=r
    return ans
radix=int(radix)
target=get(a,radix)
l=mapping[max(list(b))]
r=max(target,36)
while l<r:
    mid=(l+r)>>1
    if get(b,mid)>=target:
        r=mid
    else:
        l=mid+1
if get(b,1)==target and len(b)==1:
    print(int(b)+1)
elif get(b,r)==target:
    print(r)
else:
    print("Impossible")


活动打卡代码 AcWing 3311. 最长算术

t=int(input())
for i in range(1,t+1):
    n=int(input())
    arr=list(map(int,input().split()))+[float("inf")]
    ans=2
    left=0
    right=2
    while right<=n:
        if arr[right]-arr[right-1]!=arr[left+1]-arr[left]:
            ans=max(ans,right-left)
            left=right-1
        right+=1
    print("Case #{}: {}".format(i,ans))



题目描述

blablabla

python非递归写法(递归爆栈)

from collections import deque
m,n=map(int,input().split())
order=list(map(int,input().split()))
pre=list(map(int,input().split()))

dic={}
q=deque([[0,n-1,0,n-1,0,float("inf")]])
while q:
    pl,pr,dl,dr,d,f=q.popleft()
    root=pre[pl]
    dic[root]=[f,d]
    k=order.index(root)
    if dl<k:
        q.append([pl+1,pl+1+(k-1-dl),dl,k-1,d+1,root])
    if k<dr:
        q.append([pl+1+(k-1-dl)+1,pr,k+1,dr,d+1,root])


for _ in range(m):
    a,b=map(int,input().split())
    if a not in dic and b not in dic:
        print("ERROR: {} and {} are not found.".format(a,b))
    elif a not in dic:
        print("ERROR: {} is not found.".format(a))
    elif b not in dic:
        print("ERROR: {} is not found.".format(b))
    else:
        fa=a
        fb=b
        da=dic[a][1]
        db=dic[b][1]
        while fa!=fb:
            if da<db:
                fb=dic[fb][0]
                db-=1
            else:
                fa=dic[fa][0]
                da-=1
        if fa==a:
            print("{} is an ancestor of {}.".format(a,b))
        elif fa==b:
            print("{} is an ancestor of {}.".format(b,a))
        else:
            print("LCA of {} and {} is {}.".format(a,b,fa))



from collections import deque
m,n=map(int,input().split())
order=list(map(int,input().split()))
pre=list(map(int,input().split()))

dic={}
q=deque([[0,n-1,0,n-1,0,float("inf")]])
while q:
    pl,pr,dl,dr,d,f=q.popleft()
    root=pre[pl]
    dic[root]=[f,d]
    k=order.index(root)
    if dl<k:
        q.append([pl+1,pl+1+(k-1-dl),dl,k-1,d+1,root])
    if k<dr:
        q.append([pl+1+(k-1-dl)+1,pr,k+1,dr,d+1,root])


for _ in range(m):
    a,b=map(int,input().split())
    if a not in dic and b not in dic:
        print("ERROR: {} and {} are not found.".format(a,b))
    elif a not in dic:
        print("ERROR: {} is not found.".format(a))
    elif b not in dic:
        print("ERROR: {} is not found.".format(b))
    else:
        fa=a
        fb=b
        da=dic[a][1]
        db=dic[b][1]
        while fa!=fb:
            if da<db:
                fb=dic[fb][0]
                db-=1
            else:
                fa=dic[fa][0]
                da-=1
        if fa==a:
            print("{} is an ancestor of {}.".format(a,b))
        elif fa==b:
            print("{} is an ancestor of {}.".format(b,a))
        else:
            print("LCA of {} and {} is {}.".format(a,b,fa))


活动打卡代码 AcWing 1636. 最低公共祖先

from collections import deque
m,n=map(int,input().split())
pre=list(map(int,input().split()))
order=sorted(pre)
dic={}
q=deque([[0,n-1,0,n-1,0,float("inf")]])
while q:
    pl,pr,dl,dr,d,f=q.popleft()
    root=pre[pl]
    dic[root]=[f,d]
    k=order.index(root)
    if dl<k:
        q.append([pl+1,pl+1+(k-1-dl),dl,k-1,d+1,root])
    if k<dr:
        q.append([pl+1+(k-1-dl)+1,pr,k+1,dr,d+1,root])


for _ in range(m):
    a,b=map(int,input().split())
    if a not in dic and b not in dic:
        print("ERROR: {} and {} are not found.".format(a,b))
    elif a not in dic:
        print("ERROR: {} is not found.".format(a))
    elif b not in dic:
        print("ERROR: {} is not found.".format(b))
    else:
        fa=a
        fb=b
        da=dic[a][1]
        db=dic[b][1]
        while fa!=fb:
            if da<db:
                fb=dic[fb][0]
                db-=1
            else:
                fa=dic[fa][0]
                da-=1
        if fa==a:
            print("{} is an ancestor of {}.".format(a,b))
        elif fa==b:
            print("{} is an ancestor of {}.".format(b,a))
        else:
            print("LCA of {} and {} is {}.".format(a,b,fa))


活动打卡代码 AcWing 691. 立方体IV

from collections import deque
t=int(input())
for i in range(1,t+1):
    while True:
        s=input()
        if s:
            s=int(s)
            break
    arr=[]
    for _ in range(s):
        arr.append(list(map(int,input().split())))
    ans=1
    idx=1
    for x in range(s):
        for y in range(s):
            q=deque([[arr[x][y],x,y]])
            t=0
            while q:
                t+=1
                v,a,b=q.popleft()
                for nx,ny in [(a-1,b),(a+1,b),(a,b-1),(a,b+1)]:
                    if nx>=0 and nx<s and ny>=0 and ny<s and arr[nx][ny]==v+1:
                        q.append([v+1,nx,ny])
            if t>ans:
                idx=arr[x][y]
                ans=t
            elif t==ans and idx>arr[x][y]:
                idx=arr[x][y]
    print("Case #{}: {} {}".format(i,idx,ans))


活动打卡代码 AcWing 1631. 后序遍历

n=int(input())
pre=list(map(int,input().split()))
order=list(map(int,input().split()))

pl=0
pr=n-1
il=0
ir=n-1
while il<ir:
    root=pre[pl]
    k=order.index(root)
    if k>il:
        pl=pl+1
        pr=k-il+pl
        ir=k-1
    else:
        pl=k-il+pl+1
        il=k+1
print(order[ir])


活动打卡代码 AcWing 1631. 后序遍历

n=int(input())
pre=list(map(int,input().split()))
order=list(map(int,input().split()))

pl=0
pr=n-1
il=0
ir=n-1
while il<ir:
    root=pre[pl]
    k=order.index(root)
    if k>il:
        pl=pl+1
        pr=k-il+pl
        ir=k-1
    else:
        pl=k-il+pl+1
        il=k+1
print(order[ir])