给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。
所谓回文串,指左右对称的字符串。
所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串
数据范围:字符串长度
进阶:时间复杂度:,空间复杂度:
# 从中心向两边扩散 def centralDiffusion(s, lp, rp): while lp >= 0 and rp < len(s) and s[lp] == s[rp]: lp -= 1 rp += 1 return lp + 1, rp - 1 def maximumPalindromeSubstring(s): n = len(s) res_l, res_r = 0, 0 for i in range(n): lp1, rp1 = centralDiffusion(s, i, i) lp2, rp2 = centralDiffusion(s, i, i + 1) # 字符串为偶数的情况 if rp1 - lp1 > res_r - res_l: res_l, res_r = lp1, rp1 if rp2 - lp2 > res_r - res_l: res_l, res_r = lp2, rp2 return res_r - res_l + 1 if __name__ == '__main__': line = input() a = maximumPalindromeSubstring(line) print(a)运行时间: 40 ms 占用内存:4728K
''' 一开始我也不了解题意,以为字符串中心对称,有相等的数字对称就好,这个思路是错的 解题思路: 1、解题关键是连续的字符串正序和倒序排布后的字符串相同 2、递归轮询筛选出所有符合的字符串,然后用max得出字符串的长度最大的值 ''' while True: try: str0 = input() num = int(len(str0)) count = 0 for i in range(num): for j in range(num, i, -1): str1 = str0[i:j] if str1 == str1[::-1]: count = max(count, len(str1)) else: pass print(count) except: break
def aba(s,i,j): while i >= 0 and j < len(s) and s[i] == s[j]: i -= 1 j += 1 return s[i+1:j] while True: try: a = input() res = '' if a: for i in range(len(a)): tmp = aba(a, i, i) if len(res) < len(tmp): res = tmp tmp = aba(a,i,i+1) if len(res) < len(tmp): res = tmp print(len(res)) except: break
import sys def func(s): mmax = 1 for i in range(1, len(s)): if i-mmax >= 1 and s[i-mmax-1:i+1] == s[i-mmax-1:i+1][::-1]: mmax += 2 elif i-mmax >= 0 and s[i-mmax:i+1] == s[i-mmax:i+1][::-1]: mmax += 1 return mmax for line in sys.stdin: if line.strip(): print(func(line))
while True: try: str = input().strip() if str != '': max_palindrome = 0 for left in range(len(str)): for right in range(left + 2, len(str) + 1): reversed_str = ''.join(reversed(str[left:right])) if reversed_str in str and len(reversed_str) > max_palindrome / 2: if str.index(reversed_str) == right: max_palindrome = len(reversed_str) * 2 elif str.index(reversed_str) == right - 1: max_palindrome = len(reversed_str) * 2 - 1 print(max_palindrome) except EOFError: break
while True: try: input_s= input() if input_s: length = [0] for i in range(len(input_s)): for j in range(min(i,len(input_s)-i-1)): if input_s[i-j] == input_s[i+j+1]: length[-1] +=2 else: break length.append(0) print(max(length)) except:break没有用暴力,主要的idea是先找到相邻两个一样的字母,然后向外扩散。可能是个蠢的方法,debug了好久...估计考试的时候就gg了
def cal(s): l = len(s) temp = 0 m = [ [0] * l for _ in range(l) ] for j in range(l): for i in range(j+1): if i == j: m[i][j] = 1 elif j == i+1 and s[i] == s[j]: m[i][j] = 1 elif m[i+1][j-1] == 1 and s[i] == s[j]: m[i][j] = 1 if m[i][j] == 1: temp = max([temp, j-i+1]) return temp for s in sys.stdin: s = s.strip() if s: print(cal(s))另一种解法:
def cal(s): temp = 0 for i in range(len(s)): if i > temp and s[i-temp-1:i+1] == s[i-temp-1:i+1][::-1]: temp += 2 continue if s[i-temp:i+1] == s[i-temp:i+1][::-1]: temp += 1 return temp for s in sys.stdin: s = s.strip() if s: print(cal(s))
# 2020年11月14日20:33:52 while True: try: password = input() length = 1 change = 1 # 长度发生变化,且长度小于等于密码串总长度 while change and (length <= len(password)): for i in range(len(password)-length+1): # 若当前截取的子串也在密码串逆串中,属于题目要求的对称密码,长度发生变化 if password[i:i+length] in password[::-1]: length += 1 change = 1 break # 若当前截取的子串不在密码串逆串中,长度不变,继续下一次循环 else: change = 0 # 当长度不再变化时,输出最大对称密码长度 if change == 0: print(length-1) except: break
while True: try: string=input().strip() n=len(string) if string=='': #字符串为空时 break dp=[[0]*n ]*n maxlen=1 for l in range(n):#遍历所有可能的字符串长度 for i in range(n): j=i+l if j>= n: break if l==0:#字符串长度为1,i==j dp[i][j]= 1 elif l==1:#字符串长度为2,i+1==j if string[i]==string[j]: dp[i][j]=2 if dp[i][j]>maxlen: maxlen=dp[i][j]#记录最大长度回文 else:#其他情况 if dp[i+1][j-1]==(j-i-1)and (string[i]==string[j]): dp[i][j]=dp[i+1][j-1]+2 if dp[i][j]>maxlen: maxlen=dp[i][j]#记录最大长度回文 print(maxlen) except: break
while True: try: content = input() if content: rvsContent = content[::-1] n = 0 for i in range(len(content)): if content[i-n:i+1] in rvsContent: n += 1 print(n) except EOFError: break
def ishuiwen(s): mid = int(len(s)/2) s1 = s[mid:] if len(s)%2==0 else s[mid+1:] if s[:mid]==s1[::-1]: return True return False s = input() m = 1 for i in range(len(s)): for j in range(i+1,len(s)+1): if ishuiwen(s[i:j]): # print(j) m = max(len(s[i:j]),m) # print(len(s[i:j]),m) print(m)