原题链接:蓝桥杯2015年第六届真题-切开字符串
解题思路:暴力:遍历所有切割位置的不同A和B的所有子串,判断是否是正回文串
注意事项:1.子串是不同的!即不同位置出现的相同子串不应重复计数
2.非正回文串包括偶数回文串和非回文串,而不是仅包括偶数回文串
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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) |
2 分
2 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复