题解 | #密码截取#

密码截取

http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

import sys
def helper(s, l, r):
    while l >= 0 and r < len(s) and s[l] == s[r]:
        l -= 1
        r += 1
    return s[l+1:r]

def get_longest(s):
    res = ""
    for i in range(len(s)):
        #先判定奇数的,从i开始左右对比
        temp = helper(s, i, i)
        if len(temp) > len(res):res = temp
         #再判定偶数的,从i和i+1开始对比
        temp = helper(s, i, i + 1)
        if len(temp) > len(res):res = temp
    return len(res)

while True:
    try:
        s = input().strip()
        print(get_longest(s))
    except:
#         print(sys.exc_info())
        break












全部评论

相关推荐

专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务