阿里8.17笔试 Python解法
1.碰到很多小伙伴说python的list没办法hash,其实这里可以用一个字符串代替的。 AC 100%
n,K=map(int,input().split())
a=[]
for _ in range(n):
a.append(list(map(int,input().split())))
res=0
cache={}
for i in range(len(a)):
temp=''
t=''
for j in range(1,K):
temp+=str(a[i][j]-a[i][j-1])
t+=str(-a[i][j]+a[i][j-1])
temp+=' '
t+=' '
if t in cache:
res+=cache[t]
if temp not in cache:
cache[temp]=1
else:
cache[temp]+=1
print(res)
2.dfs+cache AC 100%
n,m=map(int,input().split())
res,cache=0,{}
def dfs(n,m):
if (n,m) in cache: return cache[(n,m)]
if m==0: return 0
if n==0: return 1
res=0
for i in range(n): res+=dfs(i,m-1)*dfs(n-i-1,m-1)
cache[(n,m)]=res
return res
print(dfs(n,m+1))

