输入一串字符串,求其最长的回文子串的长度 输入提示: "Please input string:\n" 输入格式: "%s" 输出格式: "%5d" 程序运行结果实例1: Please input string: ↙ babad 3 程序运行结果实例2: Please input string: ↙ cbbd 2
时间: 2024-02-18 12:06:27 浏览: 69
好的,我会根据您的要求进行处理。以下是您所需的 Python 代码:
```python
def longest_palindromic_substring(s: str) -> int:
n = len(s)
dp = [[False] * n for _ in range(n)]
max_len = 1
for i in range(n):
dp[i][i] = True
for j in range(1, n):
for i in range(j):
if s[i] == s[j]:
if j - i < 3:
dp[i][j] = True
else:
dp[i][j] = dp[i + 1][j - 1]
else:
dp[i][j] = False
if dp[i][j]:
max_len = max(max_len, j - i + 1)
return max_len
if __name__ == '__main__':
s = input("Please input string:\n")
length = longest_palindromic_substring(s)
print(f"{length:5d}")
```
您可以直接将以上代码复制到 Python 环境中运行,然后输入一个字符串,程序将会输出该字符串的最长回文子串的长度。希望能够帮助您!
阅读全文