题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
原理
字符串的操作判断,求最长回文串。
回文串最基本就两种:ABA型、ABBA型(奇数串和偶数串),两边循环遍历判断长度即可。时间复杂度O(n^2)
my code
while True: try: ss = input() cnt = [] for i in range(len(ss)): if i == len(ss)-1: break if ss[i] == ss[i+1]: n, x = 2, 1 while i-x>=0 and i+x+1<len(ss): if ss[i-x] == ss[i+x+1]: n+=2 x+=1 else: break cnt.append(n) elif i!=0 and ss[i-1] == ss[i+1]: n, y = 1, 1 while i-y>=0 and i+y<len(ss): if ss[i-y] == ss[i+y]: n+=2 y+=1 else: break cnt.append(n) print(max(cnt)) except: break