158C - Cd and pwd commands - CodeForces Solution


*special problem data structures implementation *1400

Please click on ads to support us..

Python Code:

n = int(input())
start = []
for _ in range(n):
    x = input().split()
    if x[0] == 'pwd':
        print(*(['/'] + start), sep='')
    else:
        m = x[1].split('/')
        if m[0] == "":
            m = m[1:]
            start = []
        for i in m:
            if i == '..':
                start.pop()
            else:
                start.append(i + '/')

C++ Code:

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll signed long long
#define ld long double
#define to_low(s) transform(s.begin(), s.end(), s.begin(), ::tolower); // convert string to lowercase
#define to_up(s) transform(s.begin(), s.end(), s.begin(), ::toupper);  // convert string to uppercase
/*------------------------------------------------------------------------------------------------------------------------------*/
vector<string> vs;
void pwd()
{
    cout << "/";
    for (vector<string>::iterator it = vs.begin(); it != vs.end(); it++)
        cout << *it << "/";
    cout << endl;
}
void cd(char path[])
{
    if (path[0] == '/')
    {
        vs.clear();
        path++;
    }
    path = strtok(path, "/");
    while (path)
    {
        string t = path;
        if (t == "..")
            vs.pop_back();
        else
            vs.push_back(t);
        path = strtok(0, "/");
    }
}
/*------------------------------------------------------------------------------------------------------------------------------*/
int main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    char cmd[5], path[250];
    for (int i = 0; i < n; ++i)
    {
        cin >> cmd;
        if (strcmp(cmd, "pwd") == 0)
            pwd();
        else
        {
            cin >> path;
            cd(path);
        }
    }
        
    return 0;
}


Comments

Submit
0 Comments
More Questions

1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes