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

news/2024/7/20 16:54:50 标签: 华为od, 开发语言

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

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











http://www.niftyadmin.cn/n/5396508.html

相关文章

一文读懂压敏电阻原理,参数,选型

大家好&#xff0c;我是砖一。 压敏电阻并不是一般的电阻&#xff0c;而是一种具有瞬态电压抑制功能的元件&#xff0c;效果同TVS。 这篇文章介绍压敏电阻的一些基本知识&#xff0c;包括参数、选型、应用等。 一&#xff0c;基础知识 压敏电阻用MY表示&#xff0c;MY后…

算法训练营day35, 二叉搜索树的范围和

package main type TreeNode struct { Val int Left *TreeNode Right *TreeNode } //938. 二叉搜索树的范围和 func rangeSumBST(root *TreeNode, low int, high int) int { sum : 0 if root nil { return sum } //中序遍历左中右处理即可 var searchBST func(node *Tr…

Spring Task的应用

介绍 Spring Task是Spring框架提供的任务调度工具&#xff0c;可以按照约定的时间自动执行某个代码逻辑。 定位&#xff1a; 定时任务框架 作用&#xff1a; 定时自动执行某段Java代码 应用场景&#xff1a; 引用卡每月还款提醒、银行贷款每月还款提醒、火车票售票系统处理未支…

区块链与Solidity详细介绍及基本语法使用

一、区块链简介 区块链是一种分布式数据库技术&#xff0c;它以块的形式存储数据&#xff0c;并通过加密算法确保数据的安全性。每个块包含一系列交易&#xff0c;并通过哈希值与前一个块相连接&#xff0c;形成一个链式结构。这种结构使得数据难以被篡改&#xff0c;因为任何对…

中文文本分类(pytorch 实现)

import torch import torch.nn as nn import torchvision from torchvision import transforms, datasets import os, PIL, pathlib, warningswarnings.filterwarnings("ignore") # 忽略警告信息# win10系统 device torch.device("cuda" if torch.cuda.i…

国外最流行的是AI,国内最流行的是AI培训教程

国外最流行的是AI&#xff0c;国内最流行的是AI培训教程。 最近李一舟AI教程事件&#xff0c;验证了这句话。 如今给客户做方案项目里能加点AI色彩&#xff0c;立项的成功率都变大(特别是事业单位)。 正因如此&#xff0c;大家都在狂补AI的知识&#xff0c;不然肚子里没点墨水&…

ubantu与windows文件传输(filezilla)

ubantu与windows文件传输&#xff08;filezilla&#xff09; windowsubantu20.04Fliezilla windows 到官网下载filezilla&#xff1a;https://www.filezilla.cn/&#xff0c;并安装。 ubantu20.04 1、安装vim sudo apt-get install vim2、安装FTP服务 sudo apt-get instal…

【C++】树形关联式容器set、multiset、map和multimap的介绍与使用

&#x1f440;樊梓慕&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》《实训项目》《C》《Linux》《算法》 &#x1f31d;每一个不曾起舞的日子&#xff0c;都是对生命的辜负 目录 前言 1.关联式容器 2.键…