【CCF】路径解析 模拟
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<vector> 7 8 using namespace std; 9 int n; 10 const int maxn=1e3+2; 11 char str[maxn]; 12 string rel; 13 void handle(){ 14 int len=strlen(str); 15 len--; 16 if(len==0){ 17 cout<<rel<<endl; 18 return; 19 } 20 string ans; 21 ans.clear(); 22 if(str[0]=='/'){ 23 ans=""; 24 }else{ 25 ans=rel; 26 } 27 int i=0; 28 while(i<len){ 29 if(str[i]=='.'&&i+1<len&&str[i+1]=='.'&&((i+2<len&&str[i+2]=='/')||(i+2==len))){ 30 int j=ans.length()-1; 31 for(;j>=0;j--){ 32 if(ans[j]=='/'){ 33 break; 34 } 35 } 36 if(j<0) ans=""; 37 else ans=ans.substr(0,j); 38 i+=2; 39 }else if(str[i]=='.'&&((i+1<len&&str[i+1]=='/')||(i+1==len))){ 40 i++; 41 }else if(str[i]=='/'){ 42 while(++i<len&&str[i]=='/'); 43 }else{ 44 string tmp="/"; 45 tmp+=str[i]; 46 while(++i<len&&str[i]!='/'){ 47 tmp+=str[i]; 48 } 49 ans+=tmp; 50 } 51 } 52 if(ans==""){ 53 printf("/\n"); 54 return; 55 }else{ 56 cout<<ans<<endl; 57 return; 58 } 59 } 60 int main(){ 61 cin>>n; 62 getchar(); 63 fgets(str,maxn,stdin); 64 rel.clear(); 65 for(int i=0;i<strlen(str)-1;i++){ 66 rel+=str[i]; 67 } 68 while(n--){ 69 fgets(str,maxn,stdin); 70 handle(); 71 } 72 return 0; 73 }