CodeForces - 158C(模拟)
题意
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;
}
CodeForces - 158C(模拟)的更多相关文章
- CodeForces 158C - Cd and pwd commands(模拟)
这个题我们又把题意理解错了,队友翻译了以后给我解释,我问这个直接一个单词开头的是要找到这个文件夹吗,他说是,然后我就呵呵了..奔着树形结构去和字符串维护就去了...做了好久都没模拟出来,感觉做出来的人 ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Codeforces 452D [模拟][贪心]
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...
- CodeForces - 796B 模拟
思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...
- CodeForces - 864C-Bus-(模拟加油站问题)
https://vjudge.net/problem/CodeForces-864C 题意:两地之间有个加油站,往返走k个单程,最少加油多少次. 大佬几十行代码就解决,我却要用一百多行的if语句模拟解 ...
随机推荐
- Sql 修改表结构
添加字段 alter table 表名 add 字段名 nvarchar(100) not null 修改字段 alter table 表名 alter column 字段名 int not null ...
- Kotlin Coroutines在Android中的实践
Coroutines在Android中的实践 前面两篇文章讲了协程的基础知识和协程的通信. 见: Kotlin Coroutines不复杂, 我来帮你理一理 Kotlin协程通信机制: Channel ...
- 程序员的进阶课-架构师之路(13)-B-树
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- Python Kite 使用教程 轻量级代码提示
1: 概述 今天升级annacoda 插件 spyder (4.0.0 )的时候 提示安装kite ,这是什么玩意? 下载下来试一试? 原来:就是一个代码提示插件.. 说白了" 就是让开发 ...
- C#程序编写高质量代码改善的157个建议【13-15】[为类型输出格式化字符串、实现浅拷贝和深拷贝、用dynamic来优化反射]
前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议13.为类型输出格式化字符串 建议14.正确实现浅拷贝和深 ...
- Yii2 负载均衡找不到JS,CSS
在部署项目的时候,用了2台服务器.请求的时候用了负载均衡,导致 YII2 的静态文件(js,css...)报 404 ,原因是: 请求一个页面时 A服务器 去处理,但是静态资源缺请求到了 B服务器 , ...
- ModelArts微认证零售客户分群知识点总结
\ 作者:华为云MVP郑永祥
- Spring代理模式(CGLIB动态代理模式)
jdk动态代理和CGLIB动态代理 没什么太大的区别,CGLIB动态代理不需要接口,但是需要导入jar包. 房东出租房子的方法: package com.bjsxt.proxy2; public cl ...
- LightOJ 1253 Misere NIM(反NIM博弈)
Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, eac ...
- CSUOJ1811 Tree Intersection (启发式合并)
Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges. The i-th vertex has color c i, ...