1040 Longest Symmetric String
题目大意:给出一个字符串,求出最长的回文子串的长度
思路:从第一个字符开始往后遍历,每次遍历第一个字符不动,从下一个字符开始找起,若是有回文子串则更新最大长度,暴力求解即可。注意这样会有个测试点超时,其中加个判断当前子串长度是否大于已经找到的最长长度,大的话则继续查找,否则直接跳到下一轮就能避免超时
def isSymmetric(s):
start,end=0,len(s)-1
while start<=end:
if s[start]!=s[end]:
return False
start+=1
end-=1
return True
s=input()
start=0
maxL=1
while start<len(s)-1:
end=start+1
while end<len(s):
length=end-start+1
if length<maxL:
end+=1
continue
if s[end]==s[start] and isSymmetric(s[start:end+1]):
maxL=max(length,maxL)
end+=1
start+=1
print(maxL)
该博客介绍了一种解决寻找字符串中最长回文子串长度的方法。通过从字符串的第一个字符开始,逐个检查并尝试扩展回文子串,如果发现回文则更新最大长度。在遍历过程中,如果当前子串长度小于已找到的最大长度,则跳过以避免超时。最终输出最长回文子串的长度。

598

被折叠的 条评论
为什么被折叠?



