【codechef】Nikhil and Commands(字符串删减,分类)

Nikhil learnt two new commands pwd and cd on the first day of Operating Systems lab.

pwd - command displays the current working directory and,
cd - changes the location of working directory.

If the cd parameter contains ".."(without quotes), that means to step one directory back.

The absolute path of directory is separated by slashes "/"(without quotes).

The default root directory is "/".

Your task is to print the current working directory.

 

Input

Input description.

  • The first line of input contains T, denoting the number of test cases.
  • The second line of input contains N, denoting the number of commands.
  • Then N lines follow, each containing either a cd command or pwd command.

Output

Output description.

  • For each pwd command, output the absolute path of current directory.

 

Constraints

Should contain all the constraints on the input data that you may have. Format it like:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100
  • Maximum length of cd parameter ≤ 200

Example

Input:
1
9
pwd
cd /home/csed
pwd
cd /lab/root/../dir
pwd
cd /home
pwd
cd lab
pwd

Output:
/
/home/csed/
/lab/dir/
/home/
/home/lab/

点击打开链接

碰到pwd就输出当前路径。出现/..就删它和前面的一个,如果开头没有‘/’则与上面的连接,否则重新开始记录。

功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
 while( result != NULL ) {
  printf( "result is \"%s\"\n", result );
   result = strtok( NULL, delims );
 }
以上代码的运行结果是:
    result is "now "
    result is " is the time for all "
    result is " good men to come to the "
    result is " aid of their country"

#include<bits/stdc++.h>
using namespace std; 
int main(){
    int tc; cin>>tc;
    while(tc--) {
      int cs;
      char s[500];
      vector<string> u;
      scanf("%d",&cs);
      while(cs--){
          scanf("%s",s);
          if(s==string("pwd")){
              for(auto& x:u) printf("/%s",x.c_str()); //琢磨一下
              puts("/");
          }else{
              scanf("%s",s);
              if(*s=='/') u.clear();
              for(char* i=strtok(s,"/");i;i=strtok(0,"/")){  //strtok的用法
                  if(i==string("..") && u.size()) u.pop_back();
                  <span style="font-size: 12.380952835083px; font-family: Arial, Helvetica, sans-serif;">if(i==string(".."))    </span>u.push_back(i);
              }
          }
      }
    }
}
#include <bits/stdc++.h> 
using namespace std; 
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		string x;
		getchar(); 
		int q=0;
		string s="/";
		while(n--){
			getline(cin,x);
			if(x=="pwd"){
				cout<<s<<endl;
			}
			else{
				x.erase(0,3);
				if(x[0]=='/') //分类
					s=x;
				else
					s+=x;
				int p;
				for(int i=0;i<s.size();++i){
					if(s[i]=='/'&&i+1<s.size()&&s[i+1]!='.')
						p=i;
					if(i>0&&s[i]=='.'&&s[i-1]=='.'){
						s=s.substr(0,p)+s.substr(i+1,s.size()-i-1);
						i=-1;
					}
				}
				s+='/';
			}
		} 
	} 
    return 0;
} 
#include<bits/stdc++.h>
#define fup(i,n) for(i=1;i<=n;i++)
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        int c,pos,i;
        cin>>c;
        string s = "/",com;
        fup(i,c) {
            cin>>com;
            if(com == "pwd") {
                if(s=="/") cout<<"/\n";
                else cout<<s<<"/"<<endl;
            }
            else if( com == "cd") {
                cin>>com;
                if(com[0] != '/') {
                    if(s=="/") s+=com;
                    else s += "/" + com;
                }
                else s = com;
                while( true)
                {
                    int k=s.find(".");
                    if(k==-1)
                        break;
                    //for(int j=0;j<=)
                    string s1=s.substr(0,k-1);
                    string s2=s.substr(k+2,s.size()-k-2); //删除中间一段只要找出两边的子串,再拼接起来就行了
                    k=s1.rfind("/");
                    s1=s1.substr(0,k);
                    s=s1+s2;
                }
            }
        }
    }
}


你可能感兴趣的:(【codechef】Nikhil and Commands(字符串删减,分类))