题解 | #最长回文子串#求最长,就从最长开始找
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
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)