题解 | #坐标移动#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import sys
#对字符串中的每一个字符遍历,首先判断是奇数回文串还是偶数回文串,再向两边拓展,判断并记录回文串的长度
def action(mm):
maxx = 0
for i in range(1,len(mm)-1):
if mm[i] == mm[i - 1]:
j = 1
while i + j <= len(mm)-1 and i-1-j >= 0 :
if mm[i+j] == mm[i-1-j]:
if maxx < 2*j + 2:
maxx = 2*j + 2
j += 1
else:
break
if mm[i] == mm[i + 1] and mm[i] == mm[i - 1]:
j = 1
while i + 1 + j <= len(mm)-1 and i-1-j >= 0 :
if mm[i+1+j] == mm[i-1-j]:
if maxx < 2*j + 3:
maxx = 2*j + 3
j += 1
else:
break
if mm[i] != mm[i + 1] and mm[i] != mm[i - 1]:
j = 1
while i + j <= len(mm)-1 and i-j >= 0 :
if mm[i+j] == mm[i-j]:
if maxx < 2*j + 1:
maxx = 2*j + 1
j += 1
else:
break
print(maxx)
for i in sys.stdin:
action(i.strip("\n"))