CCF CSP contest questions - path resolution (201604-3) (100)

Problem Description

In the operating system, data is typically stored in the file system as a file. System generally takes the form of a hierarchical organization of directories (or folders) and files composed, form a tree shape. The content file for storing data. Directory is a container, it may contain files or other directories. With a directory of the names of all files and directories are different, you can have the same name of the file or directory in different directories.

To specify a file system, you need to locate a path. In Unix-based systems (Linux, Max OS X, FreeBSD, etc.), a path consists of several parts, each part is the name of a directory or a file, with the adjacent separator / symbol between the two sections.

There is a special directory called the root directory, the root node of the tree is the entire file system is formed, is represented by a single / symbol. In the operating system, the concept of the current directory, the directory represents the user is currently working. According to the starting point of the path can be divided into two categories:

Absolute path: start with / symbol represents the path from the root directory to start construction.
Relative path: does not begin / symbol represents a path constructed from the current directory.

For example, a file system is structured as shown in FIG. In this file system, the root directory / directories, and other common d1, d2, d3, d4, and a file f1, f2, f3, f1, f4. Among them, two f1 is a file with the same name, but in different directories.

For f1 d4 files in the directory can be specified with an absolute path / d2 / d4 / f1. If the current directory is / d2 / d3, this document may use a relative path ... / d4 / f1 to specify where on ... represents a directory (note that the parent directory is the root directory itself). There Indicates catalog, for example /d1/./f1 specified is / d1 / f1. Note that, if there are a plurality of continuous / appeared, the effect is equivalent to a / , for example, / d1 /// f1 is specified / d1 / f1.

This question will give some path, for each path in the form required, after normalization given. After a path to normalization operation, which specifies the file unchanged, but will become a does not contain an absolute path and ... they do not contain multiple consecutive / symbols. If a path / end, it must be representative of a directory, the operation to remove the end of the normalization /. If this path for the root directory, the result is the normalization operation /. If the path is empty string, then the regularization result of the operation is the current directory.

Input Format

The first row contains an integer P, represents the number of paths need to be normalized operation.
The second line contains a string that represents the current directory.
The following P lines, each line containing a string representing the required path normalization operation.

Output Format

A total of P rows of a string that represents the path through the normalization operation, corresponding to the input sequence.

Sample input

7
/d2/d3
/d2/d4/f1
../d4/f1
/d1/./f1
/d1///f1
/d1/
///
/d1/../../d2

Sample Output

/d2/d4/f1
/d2/d4/f1
/d1/f1
/d1/f1
/d1
/
/d2

Evaluation scale cases and agreed with

1 ≤ P ≤ 10.
The names of files and directories contain only uppercase and lowercase letters, numbers and decimal points, minus - and underscore _.
No file or directory name is. ... or, they have a special meaning given in the description of the subject.
All paths for each input length is 1000 characters.
The current directory to ensure that input is formalized through a path after the operation.
For the first 30% of the test cases, part of the path need not comprise normalized. And ....
For the first 60% of the test cases, the need to formalize the path is an absolute path.

Written before the code

There are two caveats:

  1. The path can be an empty string, which means empty lines. Do not cin directly to the input string, use getline or other methods.
  2. The name of the directory or file is not a whole “.”or “..”, but may include '.', for example, “.d1/d2/d3/.f1”is the formal legal path.

Each value is more than two points.

Code

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

vector<string> resolution(const string &s, vector<string> &cur) {
	if (s.empty())              // 空字符串的话,返回当前目录
		return cur;
	int k = 0;
	vector<string> ans;
	if (s[0] != '/')            // 相对路径的话,从当前路径开始解析。
		ans = cur;
	bool flag = false;          // 这个flag是用来判定碰到的'.'是不是名字的一部分,默认不是。
	while (k < s.size()) {
		if (s[k] == '/') {      // 碰到'/'直接跳过。
			++k;
		} else if (s[k] == '.' && !flag) {
			if (k + 1 < s.size() && s[k + 1] == '.') {
				if (k + 2 >= s.size() || s[k + 2] == '/') {   // 是".."目录的话,往上一层
					if (!ans.empty())
						ans.pop_back();
					k += 2;
				} else {                                      // 否则,认为".."是名字的一部分。以下关于"."的部分类似。
					flag = true;
				}
			} else if (k+1 >= s.size() || s[k+1] == '/') {
				k += 2;
			} else {
				flag = true;
			}
		} else {                                             // 取出名字
			string t(1, s[k]);
			++k;
			while (k < s.size() && s[k] != '/') {
				t.append(1, s[k]);
				++k;
			}
			ans.push_back(t);
			flag = false;
		}
	}
	return ans;
}

int main() {
	int P;
	cin >> P;
	string s;
	getline(cin, s); // 这行可以换getchar。去掉一个回车符。
	getline(cin, s); // 其实没考虑过当前工作目录会是空串。
	vector<string> cur; // 用string数组存储每一层次的路径名和文件名。
	cur = resolution(s, cur);  // 当前工作目录也做一下正规化。
	for (int i = 0; i < P; ++i) {
		getline(cin, s);
		vector<string> ans = resolution(s, cur);
		for (int j = 0; j < ans.size(); ++j) {
			cout << "/" << ans[j];
		}
		if (ans.empty())
			cout << "/";
		cout << endl;
	}
	return 0;
}
发布了33 篇原创文章 · 获赞 4 · 访问量 8751

Guess you like

Origin blog.csdn.net/Touchig/article/details/85051369