华为OD机试真题-模拟项目管理-2024

#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>



std::vector<std::string> SplitString(std::string& str, const std::string& reg)
{
    std::string::size_type posLast = 0;
    std::string::size_type posCurrent = str.find(reg);

    std::vector<std::string> result;
    while (posCurrent != std::string::npos)
    {
        result.push_back(str.substr(posLast, posCurrent - posLast));
        posLast = posCurrent + reg.length();
        posCurrent = str.find(reg, posLast);
    }
    if (posLast < str.length())
    {
        result.push_back(str.substr(posLast));
    }
    return result;
}

//构建文件管理class, 每一个目录是一个节点, 每一个目录节点可能包含子目录节点
class FileSystem
{
    struct Directory
    {
        std::unordered_map<std::string, Directory*> mapChildren;
    };
    Directory* root;         //根目录
    Directory* current;      //当前目录
    std::string currentPath; //当前地址

public:
    FileSystem()
    {
        root = new Directory;
        current = root;
        currentPath="/";
    }
public:
    void Mkdir(const std::string& dir)
    {
        if (current->mapChildren.find(dir) == current->mapChildren.end())
        {
            current->mapChildren[dir] = new Directory;
        }
    }

    void Cd(const std::string& dir,const std::string& directoryTag)
    {
        if (dir == "..")
        {
            //不是根目录
            if (current != root)
            {
                int pos=currentPath.rfind(directoryTag.c_str()[0], currentPath.length() - 2);
                // /a/b/c/  ---> 最后一个/出现的位置
                currentPath= currentPath.substr(0, pos+1);
                current = root;
                std::vector<std::string> strs = SplitString(currentPath, directoryTag);
                for (const std::string& item : strs)
                {
                    if (!item.empty() && current->mapChildren.find(item) != current->mapChildren.end())
                    {
                        current = current->mapChildren[item];
                    }
                }
            }
        }
        else
        {
            if (current->mapChildren.find(dir) == current->mapChildren.end())
                return;
            currentPath = currentPath + dir + directoryTag;
            current = current->mapChildren[dir];
        }
    }
    std::string Pwd(const std::string& str)
    {
        return currentPath;
    }
};


bool CheckDirectory(const std::string& str, const std::string& directoryTag)
{
    if (str.find(directoryTag) != std::string::npos)
    {
        return false;
    }
    return true;
}

void main()
{
    std::string result;
    const std::string mkdirString = "mkdir";
    const std::string cdString = "cd";
    const std::string pwdString = "pwd";
    const std::string directoryTag="/";

    FileSystem fileSys;

    std::string str;
    while (std::getline(std::cin, str))
    {
        //如果是创建目录命令
        if (str.find(mkdirString) != std::string::npos)
        {
            std::string::size_type pos = str.find(mkdirString);
            std::string dir = str.substr(pos + mkdirString.length() + 1);
            if (CheckDirectory(dir, directoryTag))
            {
                fileSys.Mkdir(dir);
            }
        }
        else if (str.find(cdString) != std::string::npos)         //如果是打开目录命令
        {
            std::string::size_type pos = str.find(cdString);
            std::string dir = str.substr(pos + cdString.length() + 1);
            if (CheckDirectory(dir, directoryTag))
            {
                fileSys.Cd(dir, directoryTag);
            }
        }
        else if (str.find(pwdString) != std::string::npos)
        {
            std::string::size_type pos = str.find(cdString);
            std::string dir = str.substr(pos + cdString.length() + 1);
            if (CheckDirectory(dir, directoryTag))
            {
                std::string res=fileSys.Pwd(dir);
                std::cout << res << std::endl;
            }
        }
    }
}

华为OD2024 E 文章被收录于专栏

实时更新华为2024 E卷答案

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务