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

  • 相关阅读:
    组装query,query汇总,query字段
    POJ 1276, Cash Machine
    POJ 1129, Channel Allocation
    POJ 2531, Network Saboteur
    POJ 1837, Balance
    POJ 3278, Catch That Cow
    POJ 2676, Sudoku
    POJ 3126, Prime Path
    POJ 3414, Pots
    POJ 1426, Find The Multiple
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13586450.html
Copyright © 2011-2022 走看看