B站算法编程题
第一题:
def numDecodings(s): if not s: return 0 dp = [0]*(len(s)+1) dp[0] = 1 for i in range(1,len(dp)): if s[i-1] != "0": dp[i] = dp[i-1] if i != 1 and "09"<s[i-2:i]<"27": dp[i]+=dp[i-2] return dp[-1] if __name__ == '__main__': s = input() res = numDecodings(s) print(res)第二题:
def conv2d(p,k,h,w,m): o = [[0 for i in range(w-m+1)] for j in range(h-w+2)] for i in range(h-1): for j in range(w-1): temp = [s[j:j + m] for s in p[i:i+m]] res = 0 for ii in range(m): for jj in range(m): res += temp[ii][jj]*k[ii][jj] o[i][j] = res return o if __name__ == '__main__': h,w = map(int,input().rstrip().split()) p = [] for i in range(h): p.append(list(map(int,input().rstrip().split()))) m = int(input().rstrip()) k = [] for j in range(m): k.append(list(map(float,input().rstrip().split()))) o = conv2d(p,k,h,w,m) for i in range(len(o)): for j in range(len(o[0])): print(int((o[i][j])),end=" ") print() ''' 3 3 40 24 135 200 239 238 90 34 94 2 0.0 0.6 0.1 0.3 106 176 162 174 '''第三题:
def reverseword(s): length = len(s) for i in range(length//2): s[i],s[length-i-1] = s[length-i-1],s[i] return "".join(s) if __name__ == '__main__': s = input().rstrip().split(" ") if not s or len(s)==0: print ("") else: string = s[:] for i in range(len(string)): if len(string[i])%2 != 0: string[i] = reverseword(list(string[i])) res = " ".join(string) print(res)