201604-3 path resolution

Questions Number: 201604-3

questions Name: path resolution

time limit: 1.0s

Memory Limit: 256.0MB

Description of the problem

  in the operating system, data is typically stored as a file in the file system. 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 file may be specified with a relative path ../d4/f1, here .. indicates the parent 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 normalization operation, which specifies the same file, but does not contain a becomes. .. and absolute path, and does not include a plurality of continuous / 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 line 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

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

样例输入
7
/d2/d3
/d2/d4/f1
../d4/f1
/d1/./f1
/d1///f1
/d1/
///
/d1/../../d2
样例输出
/d2/d4/f1
/d2/d4/f1
/d1/f1
/d1/f1
/d1
/
/d2
评测用例规模与约定
  1 ≤ P ≤ 10。
  文件和目录的名字只包含大小写字母、数字和小数点 .、减号 - 以及下划线 _。
  不会有文件或目录的名字是 . 或 .. ,它们具有题目描述中给出的特殊含义。
  输入的所有路径每个长度不超过 1000 个字符。
  输入的当前目录保证是一个经过正规化操作后的路径。
  对于前 30% 的测试用例,需要正规化的路径的组成部分不包含 . 和 .. 。
  对于前 60% 的测试用例,需要正规化的路径都是绝对路径。

problem analysis

A simple approach is to use the first path array structure, the input (referred to as a currentpost) is stored, after the division of the input path. Specific methods are as follows Segmentation

  • If the path is not input in /the beginning, the operation returns to the previous menu indicating the presence of its path and the currentpath to form a complete
  • All /with spaces replaced
  • In one vectorconfiguration, the path each segment is added, which would be, if the returning operation on one face, the end of the pop-up element, or added
  • Finally, you can output

getline () function reads the previously input into the line breaks, resulting in an error, so that after input of n, first use getchar () reads excess "ENTER." There are also article uses cin.ingore (), it is also desirable.

#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin >> n;
  string line,s,current;
  getchar();
  getline(cin, current);
  while(n--)
  {
  	getline(cin, line);
  	if (line[0] != '/') line = current + '/' + line;
  	for (int i = 0; i < line.size(); i++){
  		if (line[i] == '/'){
  			line[i] = ' ';
  		}
  	}
  	stringstream ss(line);
  	vector<string> stack;
  	while (ss >> s) {
  		if (s == ".") continue;
  		else if (s == ".." && !stack.empty()) stack.pop_back();
  		else if (s != "..") stack.push_back(s);
  	}
  	cout << '/';
  	for (int i = 0; i < stack.size(); ++i) {
  		if (i) cout << '/';
  		cout << stack[i];
  	}
  	cout << endl;
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12635562.html