当前位置:   article > 正文

华为OD机试真题-最长子字符串的长度(一)-2023年OD统一考试(C卷)---Python3--开源_最长子字符串的长度(一)

最长子字符串的长度(一)

题目:
在这里插入图片描述

考察内容:
思路转化:求出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)










  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/466024
推荐阅读
相关标签
  

闽ICP备14008679号