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

【华为OD题库-027】代码编辑器-java

题目

某公司为了更高效的编写代码,邀请你开发一款代码编辑器程序。程序的输入为已有的代码文本和指令序列,程序需输出编辑后的最终文本。指针初始位置位于文本的开头。
支持的指令(X为大于等于0的整数,word为无空格的字符串):
FORWARD X:指针向前(右)移动X,如果指针移动位置超过了文本末尾,则将指针移动到文本未尾BACKWARD X:指针向后(左)移动X,如果指针移动位置超过了文本开头,则将指针移动到文本开头
SEARCH-FORWARD word:从指针当前位置向前查找word并将指针移动到word的起始位置,如果未找到则保持不变
SEARCH-BACKWARD word:在文本中向后查找word并将指针移动到word的起始位置,如果未找到则保持不变
INSERT word:在指针当前位置前插入word,并将指针移动到word的结尾
REPLACE word:在指针当前位置替换并插入字符(删除原有字符,并增加新的字符)
DELETE X:在指针位置删除X个字符
输入描述:
输入的第一行为命令列表的长度K
输入的第二行为文件中的原始文本接下来的K行,每行为一个指令
输出描述:
编辑后的最终结果
补充说明:
文本最长长度不超过256K
示例1
输入:
1
ello
INSERT h
输出: hello
说明: 在文本开头插入
示例2
输入:
2
hllo
FORWARD 1
INSERT e
输出: hello
说明: 在文本的第一个位置插入

示例3
输入:
2
hell
FORWARD 1000
INSERT o
输出:hello
说明: 在文本的结尾插入
示例4
输入:
1
hello
REPLACE HELLO
输出: HELLO
说明:替换
示例5
输入:
1
hello
REPLACE HELLO WORLD
输出: HELLO WORLD
说明: 超过文本长度替换
示例6
输入:
2
hell
FORWARD 100000
REPLACE o
输出: hello
说明: 超出文本长度替换

思路

简单逻辑处理题,注意边界即可
本文逻辑,index范围为0到str.length。为str.legnth时代表指针在文件末尾
注:字符串应该更好处理,转为chars数组反而麻烦很多,本文就是以chars数组实现的

题解

package hwod;

import java.util.Scanner;

public class CodeEditor {
    private static char[] chars;
    private static int index = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = Integer.parseInt(sc.nextLine());
        chars = sc.nextLine().toCharArray();
        String[] commands = new String[k];
        for (int i = 0; i < k; i++) {
            commands[i] = sc.nextLine();
        }
        codeEditor(commands);
        System.out.println(String.valueOf(chars));
    }

    private static void codeEditor(String[] commands) {
        int n = commands.length;
        for (int i = 0; i < n; i++) {
            String[] split = commands[i].split("\\s+", 2);
            String cmd = split[0];
            String val = split[1];
            switch (cmd) {
                case "FORWARD":
                    forward(Integer.parseInt(val));
                    break;
                case "BACKWARD":
                    backward(Integer.parseInt(val));
                    break;
                case "SEARCH-FORWARD":
                    searchForward(val);
                    break;
                case "SEARCH-BACKWARD":
                    searchBackward(val);
                    break;
                case "INSERT":
                    insert(val);
                    break;

                case "REPLACE":
                    replace(val);
                    break;
                case "DELETE":
                    delete(Integer.parseInt(val));
                    break;
                default:
                    System.out.println("指令不合法");
            }
        }
    }

    //向后(左)查找
    private static void searchBackward(String val) {
        char[] target_chars = val.toCharArray();
        int i = Math.min(index, chars.length - 1);
        while (i >= 0) {
            int j = target_chars.length - 1;
            while (i >= 0 && chars[i] != target_chars[j]) i--;//先找到和val最后一个字符相等的位置。
            int t = i;
            while (i >= 0 && j >= 0 && chars[i] == target_chars[j]) {
                j--;
                i--;
            }
            if (j == -1) {
                index = i + 1;
                break;
            }
            i = t;
        }

    }

    //向前(右)查找
    private static void searchForward(String val) {
        char[] target_chars = val.toCharArray();
        int i = index;
        while (i < chars.length) {
            int j = 0;
            while (i < chars.length && chars[i] != target_chars[j]) i++;//先找到和val第一个字符相等的位置。
            int t = i;
            while (i < chars.length && j < val.length() && chars[i] == target_chars[j]) {
                j++;
                i++;
            }
            if (j == val.length()) {
                index = t;
                break;
            }
            i = t;
        }
    }

    private static void insert(String val) {
        char[] newChars = new char[val.length() + chars.length];
        for (int i = 0; i < newChars.length; i++) {
            if (i < index) {
                newChars[i] = chars[i];
                continue;
            }
            if (i - index < val.length()) {
                newChars[i] = val.charAt(i - index);
            } else {
                newChars[i] = chars[i - val.length()];
            }
        }
        chars = newChars;
        index += val.length();
    }

    private static void delete(int n) {
        int curn = chars.length - index;
        if (n > curn) n = curn;
        char[] newChar = new char[chars.length - n];
        for (int i = 0; i < newChar.length; i++) {
            if (i < index) {
                newChar[i] = chars[i];
            } else {
                newChar[i] = chars[i + n];
            }
        }
        chars = newChar;
    }

    private static void replace(String val) {
        delete(val.length());
        insert(val);

    }

    private static void backward(int n) {
        index = Math.max(0, index - n);
    }

    private static void forward(int n) {
        index = Math.min(chars.length, index + n);


    }
}


推荐

如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。


http://www.kler.cn/news/133850.html

相关文章:

  • 设计模式-中介者模式-笔记
  • Perl的LWP::UserAgent库爬虫程序怎么写
  • 超大规模和隐私保护,融云如何助力 Web3 社交
  • C++ 20类型转换指南:使用场景与最佳实践
  • 软考小记-软件工程
  • 【LeetCode刷题-树】--654.最大二叉树
  • MySQL数据库索引以及使用唯一索引实现幂等性
  • 网络层——IP协议
  • 数据结构:红黑树讲解(C++)
  • Nginx负载均衡机制及常见问题
  • 16. Spring源码篇之指定构造方法参数
  • Prometheus+Grafana监控
  • 【Java并发编程七】Java内存模型
  • Django command执行脚本
  • C++初阶 日期类的实现(上)
  • 专业数据标注公司:景联文科技领航数据标注行业,满足大模型时代新需求
  • ⑩④【MySQL】什么是视图?怎么用?视图的检查选项? 视图的作用?[VIEW]
  • 【Redis】RedisTemplate最全的常用方法
  • VB.net webbrowser 自定义下载接口实现
  • 【数据结构】图的存储结构及实现(邻接表和十字链表)
  • 适用于 Windows 的 10 个最佳视频转换器:快速转换高清视频
  • C++ 字符串的 拼接,插入,查找与截取。
  • 消息消费过程
  • CnosDB有主复制演进历程
  • main.js 中的 render函数
  • 几种典型的深度学习算法:(CNN、RNN、GANS、RL)
  • S32K324 UDS Bootloader开发-下位机篇-Bootload软件(2)
  • Redis:新的3种数据类型Bitmaps、HyperLoglog、Geographic
  • SELinux零知识学习十七、SELinux策略语言之类型强制(2)
  • 日志维护库:loguru