在一行上输入一个长度为
,仅由大小写字母和数字构成的字符串
,代表截获的密码。
在一行上输出一个整数,代表最长的有效密码串的长度。
ABBA
4
在这个样例中,没有无关字符,所以最长的有效密码串就是
本身,长度为
。
12HHHHA
4
在这个样例中,最长的有效密码串是
,长度为
。
s = input() n = len(s) res = [] for i in range(0, n-1): for j in range(i, n): if s[i] == s[j] and s[i:j+1] == s[i:j+1][::-1] : res.append(len(s[i:j+1])) print(max(res))
input1 = input() def count_len(lis,idx,l,flag=0): if idx==0: return 0 for i in range(1,l+1): if lis[idx-i]==lis[idx+i+flag]: if i==l: return 2*l else: continue else: return 2*(i-1) res = 2 if input1[0]==input1[1] else 1 for i in range(1,len(input1)-1): if input1[i]==input1[i+1]: n_even = min(i,len(input1)-i-1-1) n_odd = min(i,len(input1)-i-1) res = max(res,count_len(input1,i,n_even,1)+2,count_len(input1,i,n_odd)+1) else: n = min(i,len(input1)-i-1) res = max(res,count_len(input1,i,n)+1) print(res)
def MaxSub(pwd): if len(pwd) < 1&nbs***bsp;len(pwd) > 2500: return False lenSub = [] for i in range(1,len(pwd)): for j in range(len(pwd),i,-1): sub = pwd[i:j] if sub == sub[::-1] and len(sub) > 1: lenSub.append(len(sub)) print(max(lenSub)) if __name__ == '__main__': pwd = input() MaxSub(pwd) 超时了,但想不出其他方式
# 这道题就是提取回文串 print('解法1-定轴动区间的方法:') # 求最长回文子串 line = input().strip() newline = '#'+'#'.join(line)+'#' RL = [1 for i in range(0, len(newline))] for pox in range(0, len(newline)): # 确定回文子串的中心轴的位置 for walker in range(0, pox+1): if newline[pox-walker:pox+1] == newline[pox:pox+walker+1][::-1]: RL[pox] = walker else: break # 当对称的2段字符不等时,就停止当前循环的执行,需要换一个pox maxRL = max(RL) if maxRL>=2: # 根据轴心位置与最大的轴半径 longstr = newline[RL.index(maxRL)-maxRL:RL.index(maxRL)+maxRL].replace('#','') print(longstr) print(len(longstr)) # 解法2 # 采用双指针的方法 print('解法2-采用双指针截取片段的方法:') str_input = input() if str_input == str_input[::-1]: print(len(str_input)) else: data_list = [] for i in range(len(str_input)-1): for j in range(1, len(str_input)): if str_input[i] == str_input[j] and str_input[i+1:j] == str_input[j-1:i:-1]: data_list.append(len(str_input[i:j+1])) print(max(data_list)) # 解法3-使用动态规划 print('解法3-使用动态规划:') ## 使用动态规划 nums = input().strip() n = len(nums) # 用 dp 矩阵记录nums子序列的状态 # 若 dp[i][j] = True,表示 nums[i:j+1] 为回文串 # dp[i][j] = False,表示 nums[i:j+1] 不是回文串 dp = [[False] * n for _ in range(n)] # 由于单个字符均是回文串,所以设置dp[i][j]为True。且 j>=i ,即dp的下三角全是不能用的 for i in range(n): dp[i][i] = True max_len = 1 # 初始化最大长度 start = 0 # 初始化最长回文串的起始位置 # 已经设置对角线上的状态全为 True,这里从索引 1 开始 for j in range(1, n): # 满足 j > i for i in range(j): # 如果dp[i][j] <= 2,意味着nums[i:j+1]为回文串的前提是两端相等。 if j-i <= 2: if nums[j] == nums[i]: # 更新状态 和 计算当前长度 dp[i][j] = True curent_len = j-i + 1 else: # 如果dp[i][j] > 2,意味着nums[i:j+1]为回文串的前提是两端相等 和 nums[i+1:j]也是回文串。 if nums[j] == nums[i] and dp[i+1][j-1]: dp[i][j] = True curent_len = j-i + 1 # 如果是回文串,比较当前长度和之前长度的大小,变大更新最大长度和起始位置。 if dp[i][j]: if curent_len > max_len: max_len = curent_len start = i # 通过最大长度和起始索引计算回文串。 # print(nums[start:start + max_len]) print(len(nums[start:start + max_len])) # print(max_len)
while True: try: s = input() n = len(s) if s==s[::-1]: print(n) else: maxsize = [] for i in range(n-1): for j in range(i+1,n): if s[j] == s[i] and s[i:j+1]==s[j:i-1:-1]: maxsize.append(len(s[i:j+1])) # maxsize = max(maxsize, len(s[i:j])) print(max(maxsize)) except: break
s = input() n = len(s) dp = [[0 for _ in range(n)]for _ in range(n)] dp[n-1][n-1]=1 for i in range(n-1): dp[i][i]=1 dp[i][i+1] = 2 if s[i]==s[i+1] else 1 for L in range(n-3,-1,-1): for R in range(L+2,n): temp = s[L:R+1] dp[L][R] = max(dp[L+1][R],dp[L][R-1]) if temp==temp[::-1]: dp[L][R]=max(dp[L][R],len(temp)) print(dp[0][n-1])
def judge(code, left, right): length_code = len(code) while left >= 0 and right < length_code and code[left] == code[right]: left = left - 1 right = right + 1 return right-left-1 while True: try: code = input() length_code = len(code) output = 0 for i in range(length_code): output_ABA = judge(code, i, i) output_ABBA = judge(code, i, i+1) output = max(output, output_ABA, output_ABBA) print(output) except: break