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

密码截取

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
      
                  
        

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

全部评论

相关推荐

1jian10:48h没写面评会变成这样
点赞 评论 收藏
分享
03-04 07:14
门头沟学院 C++
黑皮白袜臭脚体育生:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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