zoukankan      html  css  js  c++  java
  • CodeForces

    题意

    https://vjudge.net/problem/CodeForces-158C

    你需要实现类似 Unix / Linux 下的 cd 和 pwd 命令。

    一开始,用户处于根目录 / 下。

    对于 cd 命令,它的作用是跳转到某个路径。路径有相对路径和绝对路径,相对路径以文件夹名开头,表示当前目录下的文件夹,绝对路径以 / 开头,表示根目录下的文件夹。同时,.. 文件夹表示上一层文件夹。

    对于 pwd 命令,你需要输出当前所在的绝对路径。

    保证输入数据中所有的文件夹都存在。

    思路

    用栈记录每次往下搜索的文件夹,先对cd后面的字符串加一个"/",每次遇到../就退回上一级目录(pop)。

    具体看代码。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    #define ll long long
    const int N = 200005;
    const int mod = 1e9 + 7;
    #define lowbit(x) (x & (-x))
    int main()
    {
        std::ios::sync_with_stdio(false);
        int n;
        cin >> n;
        stack<string> st, st2;
        while (n--)
        {
            string s;
            cin >> s;
            if (s[0] == 'p')
            {
                cout << "/";
                while (!st.empty())
                {
                    st2.push(st.top());
                    st.pop();
                }
                while (!st2.empty())
                {
                    st.push(st2.top());
                    cout << st2.top();
                    st2.pop();
                }
                cout << endl;
            }
            else
            {
                string cur = "";
                cin >> s;
                s += '/';
                int l = s.length();
                for (int i = 0; i < l; i++)
                {
                    cur += s[i];
                    if (s[i] == '/')
                    {
                        if (cur == "/")
                        {
                            while (!st.empty())
                                st.pop();
                        }
                        else if (cur == "../")
                        {
                            st.pop();
                        }
                        else
                        {
                            st.push(cur);
                        }
                        cur = "";
                    }
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    鼠标拖放div 实现
    layerX offsetX pageX
    960 grid 使用
    960 grid 分析
    WebMatrix安装和使用
    Sass使用教程
    CSS预处理器实践之Sass、Less比较
    node.js 入门教程(beginnder guide
    node.js NPM 使用
    《Head First 设计模式》学习笔记——状态模式
  • 原文地址:https://www.cnblogs.com/mcq1999/p/12028083.html
Copyright © 2011-2022 走看看