题解 | #密码截取#执行速度超过90%,哈哈
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
while True:
try:
str_input = input()
# 中心为一个元素时
single_center_list,double_center_list = [1]*len(str_input), [0]*len(str_input)
# print(single_center_list)
for i in range(1, len(str_input) - 1):
j = 0
while j < min(i, len(str_input) - i - 1):
j += 1
if str_input[i-j] == str_input[i+j]:
single_center_list[i] += 2
else:
break
# print(single_center_list)
single_center_max = max(single_center_list)
# 中心为2个元素时
for i in range(1, len(str_input) - 1):
j = 0
first_flag = True
while j < min(i, len(str_input) - i - 2):
j += 1
if str_input[i] == str_input[i+1]:
if first_flag:
double_center_list[i] = 2
first_flag = False
if str_input[i-j] == str_input[i+1+j]:
double_center_list[i] += 2
else:
break
else:
break
double_center_max = max(double_center_list)
print(max(single_center_max, double_center_max))
except:
break
try:
str_input = input()
# 中心为一个元素时
single_center_list,double_center_list = [1]*len(str_input), [0]*len(str_input)
# print(single_center_list)
for i in range(1, len(str_input) - 1):
j = 0
while j < min(i, len(str_input) - i - 1):
j += 1
if str_input[i-j] == str_input[i+j]:
single_center_list[i] += 2
else:
break
# print(single_center_list)
single_center_max = max(single_center_list)
# 中心为2个元素时
for i in range(1, len(str_input) - 1):
j = 0
first_flag = True
while j < min(i, len(str_input) - i - 2):
j += 1
if str_input[i] == str_input[i+1]:
if first_flag:
double_center_list[i] = 2
first_flag = False
if str_input[i-j] == str_input[i+1+j]:
double_center_list[i] += 2
else:
break
else:
break
double_center_max = max(double_center_list)
print(max(single_center_max, double_center_max))
except:
break