首页 > 其他 > 详细

[string]Codeforces158C Cd and pwd commands

时间:2015-02-11 12:29:27      阅读:325      评论:0      收藏:0      [点我收藏+]

题意很清楚 和linux的语句是一样的 

pwd输出路径 cd进入 ..回上一层目录

此题完全是string的应用

 

遍历string:

 1 for(string::iterator it=str.begin(); it!=str.end(); it++

2      cout<<*it;

或者

 1 for(int i=0; i<str.length(); i++)

2      cout<<str.at(i);  

 

substr:

s=s.substr(a, b);  // 从s[a]开始往后b长度 赋给s

s=s.substr(a);     //  从s[a]开始到s.end() 赋给s

 

find_first_of(‘ ‘);  // 顾名思义 就是最先找到‘ ‘的位置

          没找到返回的是string::npos

find_first_not_of("ABCD"); // 最先出现除ABCD以外的字符的位置

 

s.back(); //**************

这个是C++11的,经测 codeblocks编译不通过 但在cf上用C++11交是能AC的

其作用相当于s.end()[-1];     //  最后一个字符位置 (非’/0‘) 

s.front();同back

 

 assign:

 

 

技术分享
 1 vector<string> s;
 2 int main()
 3 {
 4     int n;
 5     scanf("%d", &n);
 6     while(n--)
 7     {
 8         string cd;
 9         cin>>cd;
10         if(cd=="pwd")
11         {
12             printf("/");
13             for(int i=0;i<s.size();i++)
14                 cout<<s[i]<<"/";
15             puts("");
16         }
17         else
18         {
19             string path;
20             cin>>path;
21             if(path.end()[-1]!=/)
22                 path+=/;
23             while(path.find_first_of(/)!=string::npos)
24             {
25                 int pos=path.find_first_of(/);
26                 string a=path.substr(0, pos);
27                 path=path.substr(pos+1);
28                 if(a=="")
29                     s.clear();
30                 else if(a=="..")
31                     s.pop_back();
32                 else
33                     s.push_back(a);
34             }
35         }
36     }
37     return 0;
38 }
Codeforcs 158C

 

[string]Codeforces158C Cd and pwd commands

原文:http://www.cnblogs.com/Empress/p/4285581.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!