import sys def f(s): for le in range(len(s),1,-1): for st in range(0, len(s)-le+1): tmp=s[st:st+le] if tmp==tmp[::-1]: return(le) return(1) for line in sys.stdin: l=line.strip() print(f(l))
之前的蠢方法,从最小长度开始找:
import sys def f(s): ma=1 for le in range(2,len(s)+1): for st in range(0, len(s)-le+1): tmp=s[st:st+le] if tmp==tmp[::-1]: ma=le print(ma) for line in sys.stdin: l=line.strip() f(l)