赞
踩
题目:
考察内容:
思路转化:求出o字母出现偶次(o的索引);环形–双倍字母;
方法1:循环变量双倍字母(保证环线),记录最大偶次,如果是,则记录left和并替换left位置,并添加子字符串;
方法2:直接求出双倍字母的索引,根据最大偶次,循环遍历索引求出子字符串。
代码:
""" analyze: 环形如何实现: 偶次(0也包括) aloloboalolobo-- alolob;loboal;boalol; alolob input: 小写字母字符串 output: int, o字母出现偶数次 eg: alolobo 6 looxdolx 7 way: 把所有还有两个0的子字符串求出来 根据o的索引获取(先求出最大偶数) """ # s_temp = input() # # if "o" not in s_temp: # print(len(s_temp)) # else: # o_res = 0 # temp_list = list() # o_num = s_temp.count("o") # # 取最大偶数 # if o_num % 2 != 0: # o_num = o_num - 1 # # print(o_num) # double_s = s_temp*2 # left, right = 0, 0 # o_index = 0 # for i in range(len(double_s)): # if double_s[i] == "o": # o_res += 1 # if o_res <= o_num: # pass # # right += 1 # else: # if double_s[left:i] not in temp_list: # temp_list.append(double_s[left:i]) # left = double_s.find("o", o_index) + 1 # o_index = left # # right += 1 # o_res = 2 # elif o_res <= o_num: # pass # # right += 1 # max_str = 0 # for temp in temp_list: # max_str = max(0, len(temp)) # print(temp_list, max_str) # 优化 # s_temp = input() # # if "o" not in s_temp: # print(len(s_temp)) # else: # o_res = 0 # temp_list = list() # o_num = s_temp.count("o") # # 取最大偶数 # if o_num % 2 != 0: # o_num = o_num - 1 # double_s = s_temp * 2 # left, right = 0, 0 # o_index = 0 # for i in range(len(double_s)): # if double_s[i] == "o": # o_res += 1 # if o_res > o_num: # if double_s[left:i] not in temp_list: # temp_list.append(double_s[left:i]) # # 获取o的索引 # left = double_s.find("o", o_index) + 1 # o_index = left # o_res = 2 # max_str = 0 # for temp in temp_list: # max_str = max(0, len(temp)) # print(temp_list, max_str) # 方法根据o的索引获取 s_temp = input() if "o" not in s_temp: print(len(s_temp)) else: o_res = 0 temp_index_list = list() o_num = s_temp.count("o") o_index = 0 double_s = s_temp * 2 print(double_s) for i in range(o_num*2): index = double_s.find("o", o_index) o_index = index + 1 temp_index_list.append(index) print(temp_index_list) # 取最大偶数 if o_num % 2 != 0: o_num = o_num - 1 res = list() left, right = 0, 0 for i in range(len(temp_index_list)): if i+o_num < len(temp_index_list): res.append(double_s[left:temp_index_list[i+o_num]]) # print(double_s[left:temp_index_list[i+2]]) left = temp_index_list[i]+1 print(res)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。