Codeforces VK Cup 2012 Qualification Round 1 /158C (字符串处理)
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
C. Cd and pwd commands
http://codeforces.com/problemset/problem/158/C
time limit per test
memory limit per test
input
output
cd (change the current directory) and pwd
/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".
cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.
pwd should display the absolute path to the current directory. This path must not contain "..".
cd
Input
n (1 ≤ n ≤ 50) — the number of commands.
n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.
cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd
Directories in the file system can have the same names.
Output
pwd
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/
学英语:
The directory changes to the specified directory, starting from the current one.
从当前目录开始,变更到指定目录。
完整代码:
/*30ms,1000KB*/
#include<cstdio>
#include<cstring>
char com[4], str[5010][201], temp[201];///注意数组要开的大小!!
int main(void)
{
int n, i = 0, j;
char ch, *p;
scanf("%d", &n);
while (n--)
{
scanf("%s", com);
if (com[0] == 'p') ///pwd
{
putchar('/');
if (i)
{
for (j = 0; j < i; ++j)
{
printf("%s", str[j]);
putchar('/');
}
}
puts("");
}
else///cd
{
getchar();
ch = getchar();
if (ch == '/')
i = 0;
else if (ch == '.'){
getchar();
--i;
}
else
ungetc(ch, stdin);
gets(temp);
if (p = strtok(temp, "/"))
{
strcpy(str[i], p);
if (str[i][0] == '.')
--i;
else
++i;
}
while (p = strtok(NULL, "/"))
{
strcpy(str[i], p);
if (str[i][0] == '.')
--i;
else
++i;
}
}
}
return 0;
}
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |