1.找出串中的最长回文子串
思路:借鉴递归的思想,将字符串分为s[0:i-1],s[i],依次求出以i为末尾的最长字串,如果发现包含i的字串是回文串,将其长度与i比较,如果大于则更新,小于则丢弃,将i加一,循环以上操作,知道s[0:i]=s.
#判断是否是回文数
def huiwenshu(s):
head=0
end=len(s)-1
while(head<end):
if s[head]==s[end]:
head+=1
end-=1
else:
return False
return True
#用于求最长回文长度
def panduan(s):
max=0
j=0
for i in range(1,len(s)+1):
while(j<=i):
s1=s[j:i]
result=huiwenshu(s1)
if result==True:
if len(s1)>=max:
max=len(s1)
break
else:
break
else:
j+=1
j=0
print(max)
panduan("baaaab")