华为OD机试-2024年E卷-模拟目录管理 JAVA

图中题和这个类似:

* 题目描述
* 实现一个模拟目录管理功能的软件,输入一个命令序列,输出最后一条命令运行结果。支持命令:创建目录命令:mkdir 目录名称,如 mkdir abc 为在当前目录创建abc目录,
* 如果已存在同名目录则不执行任何操作。此命令无输出。进入目录命令:cd 目录名称,如 cd abc 为进入abc目录,特别地,cd .. 为返回上级目录,如果目录不存在则不执行任何操作。
* 此命令无输出。查看当前所在路径命令:pwd,输出当前路径字符串。约束:目录名称仅支持小写字母;mkdir 和 cd 命令的参数仅支持单个目录,
* 如:mkdir abc 和 cd abc;不支持嵌套路径和绝对路径,如 mkdir abc/efg,cd abc/efg,mkdir /abc/efg,cd /abc/efg 是不支持的。
* 目录符号为/,根目录/作为初始目录。任何不符合上述定义的无效命令不做任何处理并且无输出。
* 输入描述* 输入 N 行字符串,每一行字符串是一条命令。* 输出描述* 输出最后一条命令运行结果字符串。
* 备注* 命令行数限制100行以内,目录名称限制10个字符以内。
* 用例1
* 输入
* mkdir abc
* cd abc
* pwd
* 输出
* /abc/
* 说明:在根目录创建一个abc的目录并进入abc目录中查看当前目录路径,输出当前路径/abc/

不支持嵌套目录的写法如下:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//同OJTest2
class DirNode {
    private String dirName; //定义当前节点的目录名称
    private DirNode parent;//定义父类的Node节点
    private List<DirNode> children; //定义子类的节点列表,可能有多个同级子目录

    public DirNode(String dirName) {
        this.dirName = dirName;
        parent = null;
        children = new ArrayList<>();
    }

    public void addChildren(DirNode node) {
        children.add(node);
        node.parent = this;
    }

    public String getDirName() {
        return dirName;
    }


    public DirNode getParent() {
        return parent;
    }

    public List<DirNode> getChildren() {
        return children;
    }
}

class DirectoryDir {
    DirNode root;

    public DirectoryDir() {
        this.root = new DirNode("/");
    }

    public void mkdir(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        if (!path.contains("/")) { //如果path中没有嵌套目录的话,才能在当前root节点下创建目录
            boolean flag = false;
            for (DirNode child : root.getChildren()) {
                if (child.getDirName().equals(path)) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                DirNode node = new DirNode(path);
                root.addChildren(node);
            }
        }
    }

    public void cd(String path) {
        if (path.equals("..")) {
            if (root.getParent() != null) {
                root = root.getParent();
            }
        } else {
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            if (!path.contains("/")) {
                DirNode current = root;
                for (DirNode child : current.getChildren()) {
                    if (child.getDirName().equals(path)) {
                        current = child;
                        break;
                    }
                }
                root = current;
            }
        }
    }

    public String pwd() {
        StringBuilder bd = new StringBuilder();
        DirNode current = root;
        List<String> lists = new ArrayList<>();
        if (!current.getDirName().equals("/")) { //如果当前不在根目录
            do {
                lists.add(current.getDirName());
                current = current.getParent();
            } while (current != null);
        } else {
            return bd.append("/").toString();
        }
        for (int i = lists.size() - 1; i >= 0; i--) {
            bd.append(lists.get(i)).append("/");
        }
        return bd.substring(1);
    }
}

public class DirManage {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DirectoryDir directoryTree = new DirectoryDir();
        while (in.hasNextLine()) {
            String command = in.nextLine();
            if (command.startsWith("mkdir")) {
                String dirName = command.split(" ")[1];
                directoryTree.mkdir(dirName);
            } else if (command.startsWith("cd")) {
                String dirName = command.split(" ")[1];
                directoryTree.cd(dirName);
            } else if (command.equals("pwd")) {
                System.out.println(directoryTree.pwd());
            }
        }
    }
}

支持嵌套创建目录和嵌套cd目录的方式如下:


class DirectoryNode {
    private String dirName; //定义目录名称
    private DirectoryNode parent; //定义父节点
    private List<DirectoryNode> children; //定义孩子节点

    public DirectoryNode(String dirName) {
        this.dirName = dirName;
        this.parent = null;
        this.children = new ArrayList<>();
    }

    //添加子目录节点
    public void addChild(DirectoryNode node) {
        children.add(node);
        node.parent = this;
    }

    //获取当前目录名称
    public String getDirName() {
        return dirName;
    }

    //获取父目录节点
    public DirectoryNode getParent() {
        return parent;
    }

    //获取子目录节点
    public List<DirectoryNode> getChildren() {
        return children;
    }
}

class DirectoryTree {
    private DirectoryNode root;

    public DirectoryTree() {
        this.root = new DirectoryNode("/");
    }

    //创建目录
    public void mkdir(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        String[] dirNames = path.split("/");
        DirectoryNode current = root;
        for (String dirName : dirNames) {
            boolean found = false;
            for (DirectoryNode child : current.getChildren()) {
                if (child.getDirName().equals(dirName)) {
                    current = child;
                    found = true;
                    break;
                }
            }
            if (!found) {
                DirectoryNode newDir = new DirectoryNode(dirName);
                current.addChild(newDir);
                current = newDir;
            }
        }
    }

    public void cd(String path) {
        if (path.equals("..")) { //向后退一层目录到当前的parents层
            if (root.getParent() != null) {
                root = root.getParent();
            }
        } else { //进入到目录里
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String[] dirNames = path.split("/");
            DirectoryNode current = root;
            for (String dirName : dirNames) {
                for (DirectoryNode child : current.getChildren()) {
                    if (child.getDirName().equals(dirName)) {
                        current = child;
                        break;
                    }
                }
            }
            root = current;
        }
    }

    public String pwd() {
        StringBuilder currentPath = new StringBuilder();
        DirectoryNode current = root;
        List<DirectoryNode> pathNodes = new ArrayList<>();
        while (current != null) {
            pathNodes.add(current);
            current = current.getParent();
        }
        for (int i = pathNodes.size() - 1; i >= 0; i--) {
            currentPath.append(pathNodes.get(i).getDirName()).append("/");
        }
        return currentPath.substring(1);
    }
}

public class OJTest2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DirectoryTree directoryTree = new DirectoryTree();

        while (in.hasNextLine()) {
            String command = in.nextLine();
            if (command.startsWith("mkdir")) {
                String dirName = command.split(" ")[1];
                directoryTree.mkdir(dirName);
            } else if (command.startsWith("cd")) {
                String dirName = command.split(" ")[1];
                directoryTree.cd(dirName);
            } else if (command.equals("pwd")) {
                System.out.println(directoryTree.pwd());
            }
        }
        in.close();
    }
}

全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务