【HRBUST - 1371】 大模拟

一个简单的模拟竟然看不懂,我太菜了。

题目:Leyni OS HRBUST - 1371
链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1371
题意:

共有两种操作。一种 pwd 打印路径,第二种 cd + 路径,对已有的路径进行更改,(注意路径格式)

题解:

模拟大法,水题
注意格式

  • /x/y/z
  • /x/y/z/
  • x/y/z
  • x/y/z/
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct node{
    string s;
}s[2000];//4000有点大
void init(){//初始化
    for(int i=0;i<2000;i++){
        s[i].s="";
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int N,cnt=0;
        scanf("%d",&N);
        init();
        while(N--){
            string oper;
            cin>>oper;
            if(oper=="pwd"){
                for(int i=0;i<cnt;i++){
                    cout<<"/"<<s[i].s;
                }
                cout<<"/"<<endl;
            }
            else{
                string t;
                cin>>t;
                /* *
                 * 大坑:
                 * /x/x  需要对目录进行重新读取
                 * x/x/ 或者 /x/x/ 对目录进行重置
                 * */
                if(t[t.length()-1]=='/'){
                    init();cnt=0;
                    continue;
                }
                else t+="/";
                if(t[0]=='/'){
                    init();cnt=0;
                }
                else{
                    t='/'+t;
                }
                //把所有命令符都加载为/x/y/z/,进行模拟操作
                for(int i=0;i<t.length();i++){
                    if(t[i]=='.'&&t[i+1]=='.'){
                        cnt--;
                        if(cnt<0)cnt=0;
                        s[cnt].s="";
                        i+=2;
                    }
                    else{
                        if(t[i]=='/'){
                            if(i){
                                cnt++;
                                s[cnt].s="";
                            }
                            continue;
                        }
                        s[cnt].s+=t[i];
                    }
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Vagrant-ac/p/11872176.html