白羽啊


私信TA

用户名:723822380

访问量:2746

签 名:

等  级
排  名 4392
经  验 1639
参赛次数 0
文章发表 22
年  龄 0
在职情况 学生
学  校 泉师
专  业

  自我简介:

解题思路:暴力:遍历所有切割位置的不同A和B的所有子串,判断是否是正回文串

注意事项:1.子串是不同的!即不同位置出现的相同子串不应重复计数

              2.非正回文串包括偶数回文串和非回文串,而不是仅包括偶数回文串

参考代码:

def is_palindrome(string):#判断是否是正回文子串
    length = len(string)
    if length % 2 == 1:#仅比正常比较回文串多出一步判断长度
        for y in range(length // 2):
            if string[y] != string[length - y - 1]:
                return False
        return True
    else:
        return False


n = int(input().strip())
string = input().strip()
A_is_palindrome_list = []
B_not_palindrome_list = []

#遍历所有切法
for i in range(1, n):
    A_is_exit = []
    B_is_exit = []
    A = string[:i]
    B = string[i:]
    # print(A, B)
    A_length = len(A)
    B_length = len(B)
    count = 0
    # 遍历所有A和B的子串
    for p in range(1, A_length + 1):  # 子串长度
        for q in range(A_length - p + 1):  # 起始位置
            if is_palindrome(A[q:q + p]):
                if A[q:q + p] not in A_is_exit:#是否已经出现过
                    A_is_exit.append(A[q:q + p])
                    count += 1
                else:
                    continue
    A_is_palindrome_list.append(count)
    # print(A_is_palindrome_list)
    count = 0
    for p in range(1, B_length + 1):  # 子串长度
        for q in range(B_length - p + 1):  # 起始位置
            if not is_palindrome(B[q:q + p]):
                if B[q:q + p] not in B_is_exit:
                    B_is_exit.append(B[q:q + p])
                    count += 1
                else:
                    continue
    # print(count)
    B_not_palindrome_list.append(count)
    # print(B_not_palindrome_list)
result = max(map((lambda x, y: x * y), A_is_palindrome_list, B_not_palindrome_list))
print(result)


 

0.0分

2 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区