題意
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/117911.html
標籤:其他
