zoukankan      html  css  js  c++  java
  • 201604-3 路径解析

    实现

    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    
    using namespace std;
    
    void handle_path(vector<string>& paths, string path) {
        stringstream ss(path);
        string dir;
        bool first_dir = true;
        while (getline(ss, dir, '/')) {
            if (dir.empty()) {
                if (first_dir) {
                    paths = vector<string> ();
                }        
            } else if (dir == ".."){
                if (!paths.empty()) {
                    paths.pop_back();
                }
            } else if (dir == ".") {
                // pass
            } else {
                paths.push_back(dir);
            }
            first_dir = false;
        }
        
    }
    
    void print_formalized_path(vector<string> paths) {
        if (paths.empty()) {
            cout << "/" << endl;
            return;
        }
    
        for (vector<string>::iterator ite = paths.begin();
            ite!=paths.end();++ite) {
            cout << "/" << *ite;
        }
        cout << endl;
    }
    
    int main() {
        int num;
        cin >> num;
        cin.ignore();
    
        string cur_path;
        getline(cin,cur_path);
    
        vector<string> cur_paths, paths;
        handle_path(cur_paths, cur_path);
    
        for (int i = 0;i < num;++i) {
            string path;
            getline(cin,path);
            paths = cur_paths;
            handle_path(paths, path);
            print_formalized_path(paths);
        }
    }
    

    参考

    Pretty9

  • 相关阅读:
    django email用法
    django ImageField用法
    django集成微博内容
    python读写zip文件
    读写文件
    404渲染
    bzoj1297 / P4159 [SCOI2009]迷路
    bzoj1009 / P3193 [HNOI2008]GT考试
    poj2115 C Looooops(exgcd)
    bzoj1407 / P2421 [NOI2002]荒岛野人(exgcd)
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13586450.html
Copyright © 2011-2022 走看看