解题思路:
通过判断取出的链子中不重复元素个数来判断是否符合题意规则。


注意事项:
输入样例中有例子结尾有多余空格,需要strip()掉

参考代码:

def collect(string):
    cnt, temp = 2, 0
    for i in range(1, len(string)):
        temp = i
        if len(set(string[:i])) < 2 or \
                (len(set(string[:i])) == 2 and 'w' in string[:i]):
            cnt = len(string[:i])
        else:
            break
    if temp == len(string) - 1:
        return cnt
    if temp != len(string):
        cnt_0 = 0
        for j in range(-2, -(len(string) - temp) - 1, -1):
            if len(set(string[-1:j:-1])) < 2 or \
                    (len(set(string[-1:j:-1])) == 2 and 'w' in string[-1:j:-1]):
                cnt_0 = len(string[-1:j:-1])
            else:
                return cnt + cnt_0


n, temp = input().strip(), []
for i in range(1, len(n) - 1):
    n = n[1:] + n[0]
    temp.append(n)
print(max(map(lambda x: collect(x), temp)))


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区