为了学数位dp来的,结果debug了俩小时没找到错误在哪,在别的平台ac了两道数位dp的问题,回来继续调,还是不对,心态炸了,有没有同学可以帮忙看看哪里有问题?
def process(i,pre,is_bound,is_zero, memo, num):
'''
i: 位置
pre: 上一个位置的数字
is_bound: 是否一直贴着边(上边界)
is_zero: 是否前面都是前导0,是为True,反之False
memo: 记忆
num: 数字
'''
# base case
if i == len(num):
return 1
# 非边界情况可以直接尝试取值
if not is_bound and (i, pre) in memo.keys():
return memo[(i, pre)]
maxi = int(num[i]) if is_bound else 9
res = 0
for val in range(maxi+1):
# 当 没有前导0且当前数比前一个数字差2 或者 前面都是前导0 的时候,可以把val放置于该位置。
if (not is_zero and abs(val-pre) >= 2) or is_zero:
next_zero = is_zero and val == 0
next_bound = is_bound and val == maxi
res += process(i+1, val, next_bound, next_zero, memo, num)
if not is_bound: memo[(i, pre)] = res
return res
if __name__ == '__main__':
while True:
try:
s = input().strip().split(' ')
L, R = s[0], s[1]
print(process(0, 0, True, True, {}, R) - process(0, 0, True, True, {}, str(int(L)-1)))
except:
break