题解 | #密码截取#
密码截取
http://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
coding=utf-8
import re def get_max_dc_str(in_str): max_l = len(in_str) if max_l == 1: return 1 cut_len = max_l while cut_len > 1: for start in range(0, max_l - cut_len + 1): cut_str = in_str[start: start + cut_len] mid = cut_len / 2 if cut_len % 2 == 0: b_str = cut_str[0:mid:1] a_str = cut_str[-1:-1-mid:-1] else: # 列表会运行超时,改用切片 b_str = cut_str[0:mid+1:1] a_str = cut_str[-1:-1-mid-1:-1]
if cut_len == 7:
print 'cut_str: %s, b_str: %s, a_str: %s' % (cut_str, b_str, a_str)
if b_str == a_str:
print cut_str
return cut_len
cut_len -= 1
else:
return 1
import time
while True:
try:
# 对所有的字符进行转大写或转小写
in_str = raw_input().upper()
# 根据所有的非字符的进行分割
dc_lst = [i for i in re.findall('[A-Z]*', in_str) if i != '']
# 从最长到最短的方式查找对称字符串。从左-中间 = 从右-中间
max_cut = 0
for dc in dc_lst:
res = get_max_dc_str(dc)
time4 = time.time()
print 'get_max_dc_str: ', time4 - time3
time3 = time4
print 'res: ', res
if res > max_cut:
max_cut = res
print max_lst
print max_cut
except BaseException as e:
print e
break