题解 | #最长回文子串#
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
#参考:https://leetcode.cn/problems/longest-palindromic-substring/solution/dai-ma-sui-xiang-lu-5-zui-chang-hui-wen-kgyl1/ #DP maxlen =0 left =0 right=0 text = input() length = len(text) dp=[[False]*length for _ in range(length)] #dp[i][j]表示text[i:j]是否是回文 for i in range(length): dp[i][i] = True for i in range(length-1,-1,-1): for j in range(length): if text[i] == text[j]: if j-i < 3: dp[i][j]=True else: dp[i][j]=dp[i+1][j-1] if dp[i][j] and j-i+1>maxlen: left = i right = j maxlen=j-i+1 print(maxlen)