题解 | 密码截取
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
string = input() dp = [[False] * len(string) for _ in range(len(string))] max_len = 0 for i in range(len(string)-1,-1,-1): for j in range(i,len(string)): if string[i] == string[j] and (j - i <= 1 or dp[i+1][j-1]): dp[i][j] = True max_len = max(max_len,j-i+1) print(max_len)
通过动态规划判断回文字串数量的方法,每次都存最大长度