VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

C. Cd and pwd commands

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/158/C

Description

Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).

Directories in Vasya‘s operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".

The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.

The command pwd should display the absolute path to the current directory. This path must not contain "..".

Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.

Input

The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands.

Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.

The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive.

Directories in the file system can have the same names.

Output

For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.

Sample Input

7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd

Sample Output

/
/home/vasya/
/home/
/home/petya/

HINT

题意

俩操作

cd 改变路径

pwd输出路径

..就是返回到上一级

题解:

模拟一下就好了~

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
//**************************************************************************************

int n,z;
string c,s,p="/";
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>c;
        if(c=="cd")
        {
            cin>>c;
            c+="/";
            if(c[0]==‘/‘)
            {
                 p="/";
                 z=1;
            }
            for(int j=z;j<c.size();j++)
            {
                if(c[j]==‘.‘&&c[j+1]==‘.‘&&c[j+2]==‘/‘)
                {
                    int k=p.size()-2;
                    p[k+1]=‘\0‘;
                    p.resize(p.size()-1);
                    while(p[k]!=‘/‘)
                    {
                        p[k]=‘\0‘;
                        k--;
                        p.resize(p.size()-1);
                    }
                    j+=2;
                }
                else
                {
                    p+=c[j];
                }

            }
            z=0;
        }
        if(c=="pwd")
            cout<<p<<endl;
    }

}
时间: 2024-10-06 04:03:43

VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟的相关文章

DP VK Cup 2012 Qualification Round D. Palindrome pairs

题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 1 /* 2 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 3 DP:很巧妙的从i出发向两头扩展判断是否相同来找回文串 4 dpr[i] 代表记录从0到i间的回文子串的个数,dpl[i] 代表记录i之后的回文子串个数 5 两两相乘就行了 6 详细解释:http://blog.csdn.net/shiyuankongbu/article/details

VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Description One popular website developed an unusual username editing procedure. One can change the username only by deleting some characte

VK Cup 2016 - Qualification Round 1 - D. Running with Obstacles

题意 : 在 x 坐标轴上,从 0 到 m 点,中途有 n 个障碍,遇到障碍可以 跳,但是每次跳之前需要一段 距离为 s 的“助跑 ”,而且每次跳跃距离不能超过 d ,不能落在障碍点上:给出 n,m,s,d,接下来 n 个数 ,表示障碍的坐标.输入保证起点和终点不会有障碍,不会有两个障碍在同一位置. 输出到达终点的过程.如果不能到达终点输出“IMPOSSIBLE” . 解题: 模拟啊模拟(好烦好烦= =) 首先 他一定是跑到距离障碍最近的位置才跳的,这样可以跳的更远些.然后两个障碍之间相距 要大

[树形DP]VK Cup 2012 Round 1 D. Distance in Tree

题意: 给出一棵树,然后问任意两点间距离为k的情况有多少种. 分析: 显然是DP,但是状态方程如何向呢?一棵树,肯定是先从根节点开始考虑情况,那么就把每个点看做是一课子树,然后dp[i][j] 表示计算到i点时距离为k的情况的种类数.然后扫描该点的子节点,递归,完了之后 ans+=dp[x][j-1]*dp[v][k-j]; 表示到i节点的距离和到子节点中的距离==k-1的所有情况,为什么是k-1 ,而不是k呢,因为x和子节点之间还有一条边的长度,然后更新x节点. 这里累加答案和更新x的顺序很重

【树形dp】VK Cup 2012 Round 1 D. Distance in Tree

统计树中长度为K的路径条数. 用f[u][k]表示从u结点的子树中出发,终止于u结点的长度为k的路径条数. 边dp边统计答案.为了防止重复统计,在枚举子节点的时候,先将该子节点和当前u结点(和前面已经统计过的子节点)的dp值统计到ans以后, 再把该子节点的dp值加到u结点的dp值中去. 这样,我们统计经过某个结点的长度为K的路径条数的时候,可以保证路径的两个端点来自其两个不同的子树或其本身,这样也是为了防止重复统计. #include<cstdio> using namespace std;

Facebook Hacker Cup 2018 Qualification Round

25: Tourist 暴力 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 string str[111]; 5 vector<string> v; 6 map<string, int> mp; 7 bool cmp(string a, string b) { 8 return mp[a] < mp[b]; 9 } 10 11 int main() { 12

Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine)

题目链接:https://codeforces.com/contest/1315 A - Dead Pixel 诚心诚意的送分题. 题意:给一个n*m的格子矩阵,和一个坏掉的格子,找一个最大的不包含坏掉的格子的矩阵. void test_case() { int n, m, x, y; scanf("%d%d%d%d", &n, &m, &x, &y); int ans = max(x * m, (n - (x + 1)) * m); ans = max

[string]Codeforces158C Cd and pwd commands

题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 遍历string: 1 for(string::iterator it=str.begin(); it!=str.end(); it++ 2      cout<<*it; 或者 1 for(int i=0; i<str.length(); i++) 2      cout<<str.at(i); substr: s=s.substr(a, b);  // 从s[

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g