• csp 201604-3路径解析


    csp 201604-3路径解析
    一道简单模拟,主要是读入,这题如果会读入就太简单了
    用stringstream
    path="/d1/d2/d3"
    stringstream ss(path);
    while(getline(ss,dir,'/'))
    的结果就是
    “”//第一次为空,因为上来就是‘/’
    d1
    d2
    d3
    就是每次读到'/'就停,并且把之前读的放到dir里,dir也是string类型的

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <stack>
    #include <cstring>
    #include <sstream>
    #define inf 2147483647
    #define N 1000010
    #define p(a) putchar(a)
    #define For(i,a,b) for(int i=a;i<=b;++i)
    
    using namespace std;
    int T,cnt;
    stack<string>s,k,temp;
    string ls,path,dir;
    void in(int &x){
        int y=1;char c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
        while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
        x*=y;
    }
    void o(int x){
        if(x<0){p('-');x=-x;}
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void deal(stack<string> &t,string path){
        stringstream ss(path);
        cnt=0;
        while(getline(ss,dir,'/')){
            ++cnt;
            if(dir.empty()){
                if(cnt==1)
                    while(!t.empty()) t.pop();
            }
            else if(dir==".."){
                if(!t.empty()) t.pop();
            }
            else if(dir!=".") t.push(dir);
        }
    }
    
    void print(){
        //p('
    ');
        while(!k.empty()){
            temp.push(k.top());
            k.pop();
        }
        if(temp.empty()) p('/');
        else{
            while(!temp.empty()){
                cout<<'/'<<temp.top();
                temp.pop();
            }
        }
        p('
    ');
    }
    
    signed main(){
        in(T);
        getline(cin,ls);
        deal(s,ls);
        while(T--){
            getline(cin,ls);
            k=s;
            deal(k,ls);
            print();
        }
        return 0;
    }
  • 相关阅读:
    Mac eclipse导入项目中文乱码问题解决
    初识Freemarker
    Mac 导入maven项目详解
    Mac iTerm2使用总结
    HTML学习笔记——标签
    仿QQ大战—界面篇
    Java之类的构造器(反射)
    JAVA之IO流(字符流)
    JAVA之IO流(字节流)
    仿QQ大战—服务器的搭建(ServerSocket)
  • 原文地址:https://www.cnblogs.com/war1111/p/12626569.html
Copyright © 2020-2023  润新知