#别人的聪明办法: #1.巧用[::-1] #2.使用两个for循环,第二个循环从i+1开始 while True: try: s = input() res = [] for i in range(len(s)): for j in range(i+1, len(s)+1): if s[i:j] == s[i:j][::-1]: res.append(j-i) if res != '': print(max(res)) except: break #我的笨办法: s=[x for x in input()] a=0 for i in range(len(s)-1): c=0 #假设是abba类型的回文 if s[i]==s[i+1]: c=2 for j in range(1,min(i+1,len(s)-i-1)): if s[i-j]==s[i+1+j]: c=c+2 else: break #假设是bab类型的回文,利用insert在中间插入一个a,变成了baab型的回文再计算 else: s1=s[:] s1.insert(i+1, s[i]) c=1 for j in range(1,min(i+1,len(s)-i-1)): if s1[i-j]==s1[i+1+j]: c=c+2 else: break if c>a: a=c print(a)