CodeForces 158C.Cd and pwd command

本文介绍了一个模拟Linux中Cd和Pwd命令的程序实现。通过字符串操作处理路径变化,支持绝对路径和相对路径转换,实现目录的切换和显示功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://codeforces.com/problemset/problem/158/C

C. Cd and pwd commands

time limit pertest  3 seconds

memory limit pertest  256 megabytes

input  standardinput

output  standardoutput

 

Vasya is writing anoperating system shell, and it should have commands for workingwith directories. To begin with, he decided to go with just twocommands: cd (change the currentdirectory) and pwd (display thecurrent directory).

Directories inVasya's operating system form a traditional hierarchical treestructure. There is a single root directory, denoted by the slashcharacter "/". Every other directory has a name — a non-emptystring consisting of lowercase Latin letters. Each directory(except for the root) has a parent directory — the one thatcontains the given directory. It is denoted as "..".

Thecommand cd takes a singleparameter, which is a path in the file system. The command changesthe current directory to the directory specified by the path. Thepath consists of the names of directories separated by slashes. Thename of the directory can be "..", which means a step up to theparent directory. «..» can be used in any place of the path, maybeseveral times. If the path begins with a slash, it is considered tobe an absolute path, that is, the directory changes to thespecified one, starting from the root. If the parameter begins witha directory name (or ".."), it is considered to be a relative path,that is, the directory changes to the specified directory, startingfrom the current one.

Thecommand pwd should display theabsolute path to the current directory. This path must not contain"..".

Initially, thecurrent directory is the root. All directories mentioned explicitlyor passed indirectly within anycommand cd are considered toexist. It is guaranteed that there is no attempt of transition tothe parent directory of the root directory.

 

Input

The first line of theinput data contains the singleinteger (1 ≤ n ≤ 50) — thenumber of commands.

Thenfollow lines, each contains onecommand. Each of these lines contains eithercommand pwd, or command cd,followed by a space-separated non-empty parameter.

The commandparameter cd only contains lowercase Latin letters, slashes and dots, two slashes cannot goconsecutively, dots occur only as the name of a parentpseudo-directory. The commandparameter cd does not end with aslash, except when it is the only symbol that points to the rootdirectory. The command parameter has a length from 1 to 200characters, inclusive.

Directories in thefile system can have the same names.

 

Output

For eachcommand pwd you should print thefull absolute path of the given directory, ending with a slash. Itshould start with a slash and contain the list of slash-separateddirectories in the order of being nested from the root to thecurrent folder. It should contain no dots.

 

Sample test(s)

input

7

pwd

cd /home/vasya

pwd

cd ..

pwd

cd vasya/../petya

pwd

output

/

/home/vasya/

/home/

/home/petya/

input

4

cd /a/b

pwd

cd ../a/b

pwd

output

/a/b/

/a/a/b/

 

原文:http://www.xuebuyuan.com/576977.html

解题说明:这题模拟了linux下对文件路径的操作,包括.. /这些符号的使用。按照题目要求,肯定是输入一条数据就处理一条数据,处理的意思就是确定当前的路径。一个很容易想到的方法是用树来实现,不过用在这种题目上过于复杂了。这题完全可以用string来模拟,用一个string来保存当前的路径,中间用/隔开,当出现../时回退到上一个/所在处,否则就把读取到的值放入string,可能是覆盖或是拼接。

 

#include <iostream>
#include<string>
using namespace std;

int main()
{
	string temp,pr,st;		//temp用来保存当前截取的字符串,pr用于输出,st用于输入
	int i,j,n,len;

	scanf("%d",&n);

	pr="/";					//pr初始化
	temp="";				//temp初始化
	while (n--)
	{
		cin>>st;
		if (st=="cd")
		{
			cin>>st;
			st+='/';
			len=st.length();
			for (i=0;i<len;i++)
			{
				temp+=st[i];				//每次截取st子字符串,遇到'/'时停下
				if (st[i]=='/')
				{
					if (temp=="/")			 //初始化pr为根目录
						pr=temp;
					else
					if (temp=="../")		//返回上级目录
					{
						for (j=pr.length()-1;pr[j-1]!='/';j--);
						pr.resize(j);
					}
					else
						pr+=temp;
					temp="";
				}
			}
		}
		else 
			cout<<pr<<endl;
	}

	return 0;
}


 

注意事项:

string是类,不能使用printf输出,printf()只能打印基本类型。

应该用

string s;

cout << s;

printf("%s", s.c_str()); //不推荐

 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值