当前位置: 首页 > news >正文

华为OD机试真题 新系统【小学英语老师批改作文】

小学英语老师批改作文(C/C++/Py/Java/Js/Go)

华为OD机试新系统真题 华为OD上机考试新系统真题 5月20号 100分题型

华为OD机试新系统真题目录点击查看: 华为OD机试新系统真题题库目录|机考题库 + 算法考点详解

题目描述

你是一名小学英语老师,正在批改学生的英语作文。由于学生在书写单词时常常会出现一些小问题,比如多余的空格,作文批改过程中需要纠正问题,包括前后多余空格去除,中间重复的空格应该删除多余空格,单词间最多只保留一个空格。
为了简化查找过程,重复字符比较忽略字符大小写,am等于AmAMaM

输入描述

输入一个仅包含 ASCII 字符的文本字符串 ​story​

输出描述

请你找出批改后的作文中,最长的不包含重复字符的子串长度。

约束

0 ≤ s t o r y . l e n g t h ≤ 1000 0 \le story.length \le 10000story.length1000

story​ 仅包含 ​ASCII 字符(​0-127​)。

样例1

输入

Hello World!

输出

7

说明

最长子串为World!​,长度为 ​7​。

样例2

输入

hi, jIn

输出

5

说明

先对空格和大小写做处理,输入串等价为Hi, Jin,最长子串是", Jin",长度为5 55

题解

思路:滑动窗口

  1. 预处理,将输入字符串中首尾空格去除,大写字母统一转换为小写两个单词分割间多余一个的空格去除,从而得到真正要处理的字符串。这一步处理过程中主要注意空字符串全为空格和正确处理连续空格边界情况就行。
  2. 得到真正要处理的字符串str, 如果str为空直接返回0就行。对于非空字符串求最长连续不重复子串长度采用滑动窗口进行处理。具体处理逻辑如下:
    1. 初始定义left = 0, 表示合法子串的左边界,定义index[128]用于保存离右边界最近指定字符的坐标。定义res = 0, 存储最长连续不重复子串长度
    2. 接下来right从前进行枚举,当前字符为c = str[right]的处理逻辑为:
      1. 如果index[c] >= left, 如果窗口中出现重复字符串,左边界需要移动到left = index[c] + 1才能保证子串不重复。
      2. index[c] < left不做处理,[left, right]字符不重复。
      3. 尝试更新结果res = max(res, right - left + 1)
      4. 并更新index[c] = right
    3. 按照2的逻辑进行处理,即可得到正确结果。

算法时间复杂度为O(n)

c++

#include<iostream> #include<vector> #include<string> #include <utility> #include <sstream> #include<algorithm> #include<cmath> #include<map> using namespace std; // 大写转换为小写并去除首尾空格和中间多余空格 string handleStr(string& story) { string tmp; int n = story.size(); // 确定不包含首尾空格的起始和结束点 int begin = 0; int end = n - 1; while (begin < n - 1&& story[begin] == ' ') { begin++; } while (end >= 0 && story[end] == ' ') { end--; } // 全空格情况 if (begin > end) { return ""; } for (int i = begin; i <= end; i++) { char c = story[i]; // 大写转小写 if ('A' <= c && c<='Z') { c = c + 32; } //去除连续空格 if (c == ' ' && tmp.back() == ' ') { continue; } tmp.push_back(c); } return tmp; } int getLengthSubStr(string& story) { string str = handleStr(story); vector<int> index(128, -1); int left = 0; int res = 0; // 滑动窗口求最大不重复子串长度 for (int right = 0; right < str.size(); right++) { char c = str[right]; if (index[c] != -1) { if (left <= index[c]) { left = index[c] + 1; } } index[c] = right; res = max(res, right - left + 1); } return res; } int main() { string story; getline(cin, story); int res = getLengthSubStr(story); cout << res; return 0; }

JAVA

import java.util.*; public class Main { // 大写转换为小写并去除首尾空格和中间多余空格 public static String handleStr(String story) { StringBuilder tmp = new StringBuilder(); int n = story.length(); // 确定不包含首尾空格的起始和结束点 int begin = 0; int end = n - 1; while (begin < n - 1 && story.charAt(begin) == ' ') { begin++; } while (end >= 0 && story.charAt(end) == ' ') { end--; } // 全空格情况 if (begin > end) { return ""; } for (int i = begin; i <= end; i++) { char c = story.charAt(i); // 大写转小写 if ('A' <= c && c <= 'Z') { c = (char)(c + 32); } // 去除连续空格 if (c == ' ' && tmp.length() > 0 && tmp.charAt(tmp.length() - 1) == ' ') { continue; } tmp.append(c); } return tmp.toString(); } public static int getLengthSubStr(String story) { String str = handleStr(story); int[] index = new int[128]; Arrays.fill(index, -1); int left = 0; int res = 0; // 滑动窗口求最大不重复子串长度 for (int right = 0; right < str.length(); right++) { char c = str.charAt(right); if (index[c] != -1) { if (left <= index[c]) { left = index[c] + 1; } } index[c] = right; res = Math.max(res, right - left + 1); } return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String story = sc.nextLine(); int res = getLengthSubStr(story); System.out.print(res); } }

Python

# 大写转换为小写并去除首尾空格和中间多余空格defhandleStr(story):tmp=[]n=len(story)# 确定不包含首尾空格的起始和结束点begin=0end=n-1whilebegin<n-1andstory[begin]==' ':begin+=1whileend>=0andstory[end]==' ':end-=1# 全空格情况ifbegin>end:return""foriinrange(begin,end+1):c=story[i]# 大写转小写if'A'<=c<='Z':c=chr(ord(c)+32)# 去除连续空格ifc==' 'andlen(tmp)>0andtmp[-1]==' ':continuetmp.append(c)return''.join(tmp)defgetLengthSubStr(story):s=handleStr(story)index=[-1]*128left=0res=0# 滑动窗口求最大不重复子串长度forrightinrange(len(s)):c=s[right]ifindex[ord(c)]!=-1:ifleft<=index[ord(c)]:left=index[ord(c)]+1index[ord(c)]=right res=max(res,right-left+1)returnres story=input()res=getLengthSubStr(story)print(res)

JavaScript

constreadline=require("readline");constrl=readline.createInterface({input:process.stdin,output:process.stdout});// 大写转换为小写并去除首尾空格和中间多余空格functionhandleStr(story){lettmp="";constn=story.length;// 确定不包含首尾空格的起始和结束点letbegin=0;letend=n-1;while(begin<n-1&&story[begin]===' '){begin++;}while(end>=0&&story[end]===' '){end--;}// 全空格情况if(begin>end){return"";}for(leti=begin;i<=end;i++){letc=story[i];// 大写转小写if('A'<=c&&c<='Z'){c=c.toLowerCase();}// 去除连续空格if(c===' '&&tmp.length>0&&tmp[tmp.length-1]===' '){continue;}tmp+=c;}returntmp;}functiongetLengthSubStr(story){conststr=handleStr(story);constindex=newArray(128).fill(-1);letleft=0;letres=0;// 滑动窗口求最大不重复子串长度for(letright=0;right<str.length;right++){constc=str[right];if(index[c.charCodeAt(0)]!==-1){if(left<=index[c.charCodeAt(0)]){left=index[c.charCodeAt(0)]+1;}}index[c.charCodeAt(0)]=right;res=Math.max(res,right-left+1);}returnres;}rl.on("line",function(line){constres=getLengthSubStr(line);console.log(res);rl.close();});

Go

packagemainimport("bufio""fmt""os")// 大写转换为小写并去除首尾空格和中间多余空格funchandleStr(storystring)string{tmp:=make([]byte,0)n:=len(story)// 确定不包含首尾空格的起始和结束点begin:=0end:=n-1forbegin<n-1&&story[begin]==' '{begin++}forend>=0&&story[end]==' '{end--}// 全空格情况ifbegin>end{return""}fori:=begin;i<=end;i++{c:=story[i]// 大写转小写if'A'<=c&&c<='Z'{c=c+32}// 去除连续空格ifc==' '&&len(tmp)>0&&tmp[len(tmp)-1]==' '{continue}tmp=append(tmp,c)}returnstring(tmp)}funcgetLengthSubStr(storystring)int{str:=handleStr(story)index:=make([]int,128)fori:=0;i<128;i++{index[i]=-1}left:=0res:=0// 滑动窗口求最大不重复子串长度forright:=0;right<len(str);right++{c:=str[right]ifindex[c]!=-1{ifleft<=index[c]{left=index[c]+1}}index[c]=rightifright-left+1>res{res=right-left+1}}returnres}funcmain(){reader:=bufio.NewReader(os.Stdin)story,_:=reader.ReadString('\n')// 去掉换行iflen(story)>0&&story[len(story)-1]=='\n'{story=story[:len(story)-1]}res:=getLengthSubStr(story)fmt.Print(res)}

C语言

#include<stdio.h>#include<string.h>chartmp[1005];// 大写转换为小写并去除首尾空格和中间多余空格voidhandleStr(char*story){intn=strlen(story);// 确定不包含首尾空格的起始和结束点intbegin=0;intend=n-1;while(begin<n-1&&story[begin]==' '){begin++;}while(end>=0&&story[end]==' '){end--;}// 全空格情况if(begin>end){tmp[0]='\0';return;}intidx=0;for(inti=begin;i<=end;i++){charc=story[i];// 大写转小写if('A'<=c&&c<='Z'){c=c+32;}// 去除连续空格if(c==' '&&idx>0&&tmp[idx-1]==' '){continue;}tmp[idx++]=c;}tmp[idx]='\0';}intgetLengthSubStr(char*story){handleStr(story);intindex[128];for(inti=0;i<128;i++){index[i]=-1;}intleft=0;intres=0;// 滑动窗口求最大不重复子串长度for(intright=0;tmp[right]!='\0';right++){charc=tmp[right];if(index[(int)c]!=-1){if(left<=index[(int)c]){left=index[(int)c]+1;}}index[(int)c]=right;if(right-left+1>res){res=right-left+1;}}returnres;}intmain(){charstory[1005];fgets(story,sizeof(story),stdin);intlen=strlen(story);// 去掉换行符if(len>0&&story[len-1]=='\n'){story[len-1]='\0';}intres=getLengthSubStr(story);printf("%d",res);return0;}
http://www.cnnetsun.cn/news/2495170.html

相关文章:

  • 每天节省25分钟:淘宝淘金币自动化脚本全攻略
  • USBIPD-Win终极指南:3步实现Windows与WSL 2的USB设备无缝共享
  • 如何在IDEA中高效编辑JAR文件:JarEditor插件完整指南
  • AhabAssistantLimbusCompany:PC端《Limbus Company》自动化助手终极指南
  • 快速制作系统启动盘:Rufus实战指南与高级配置技巧
  • AI编程助手的现状与未来:Copilot、CodeLlama与GPT-4
  • Cursor Free VIP技术架构深度解析:设备标识重置与多平台兼容实现
  • 如何在GTA V中安全使用YimMenu:新手完全指南与防封技巧
  • ElevenLabs东北话语音API调用失败率高达41%?一线工程师紧急封存的6个底层HTTP头配置方案
  • Obsidian Full Calendar插件完整指南:在笔记中高效管理你的日程
  • 告别数据锁定:用youdaonote-pull实现有道云笔记的本地化自由
  • 5分钟搞定歌词管理:LDDC免费歌词下载工具完全指南
  • cann/asc-devkit:内置数据类型
  • 如何通过CDCS项目快速提升数据科学实战能力:中国数据竞赛优胜解集锦的终极指南 [特殊字符]
  • TextShot多语言OCR配置指南:如何轻松识别中文、英文、法文等100+语言
  • requests-oauthlib实战:构建完整的第三方应用集成方案
  • fltk-rs主题定制技巧:打造个性化GUI界面的10个实用方法
  • 如何在Windows上快速运行安卓应用:APK Installer终极指南
  • 如何高效使用Mihon漫画阅读器:Android平台上的开源漫画管理解决方案
  • 如何为老款Mac安装最新macOS?OCLP-Mod技术深度解析
  • 5分钟快速搭建Windows RTMP流媒体服务器:新手完整指南
  • Axure RP 中文语言包:3分钟告别英文界面困扰
  • OpCore-Simplify:10分钟搞定黑苹果配置的终极指南
  • 开发AI应用时如何利用Taotoken实现多模型降级容灾策略
  • 终极指南:如何快速配置org-brain概念映射工具
  • 如何在Windows电脑上高效刷酷安?酷安UWP终极指南帮你告别小屏时代
  • Android流式布局FlowLayout
  • 如何快速配置Live Server Web Extension:提升开发效率的完整指南
  • 4大核心功能解析:Bifrost跨平台三星固件管理工具的革新之道
  • 一键预览文件夹:Windows文件管理的终极效率革命