题解 | 中心扩展法#密码截取#

密码截取

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

def spread(left,right):
    ans = 0
    global n
    global s
    while left >=0 and right<= n-1 and s[left]==s[right]:
        ans = right-left+1
        left -=1
        right +=1
    return ans

while True:
    try:
        s = input()
        max_ans = 0
        n = len(s)
        for i in range(n):
            ans1 = spread(i, i)
            ans2 = spread(i, i+1)
            max_ans=max(ans1,ans2,max_ans)
        print(max_ans)
    except:
        break
      
                  
        

这里需要注意中心点可能一个字符也可能是两个字符。

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 18:05
何尝不是一种学历歧视呢
码农索隆:楼主明确拒绝,并说明拒绝原因了,这hr倒是挺忠心护主的
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务