ccf字符串題

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
    int num;
    string curDir;
    vector<string> list;

    cin>>num>>curDir;
    cin.ignore();
    for(int i=0; i<num; i++){
        string line;
        getline(cin,line);
        if(line == ""){
            list.push_back(curDir); continue;
        }
        int pos;
        // 除去多個///
        while( (pos=line.find("//"))!=-1 ){
            int count = 2;
            while(line[pos+count]=='/'){
                count++;
            }
            line.erase(pos,count-1);
        }
        // 除去../
        while( (pos=line.find("../"))!=-1 ){
            if(line[0]=='/'){
                if(pos==1){
                    line.erase(pos,3);
                }else{
                     int spos;
                    spos = line.rfind("/",pos-2);//相當於倒着找上一個/,然後整體刪除 
                    line.erase(spos,pos-spos+2);
                }
            }else{
                line = curDir+"/"+line;
            }
            //cout <<" line = " << line << endl;

        }
        // 除去./
        while( (pos=line.find("./"))!=-1 ){
            line.erase(pos,2);
        }
        if(line.size()>1 && line[line.size()-1]=='/')
            line.erase(line.size()-1);
        list.push_back(line);
    }
    for(int j=0; j<num; j++){
        cout<<list[j]<<endl;
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章