总时间限制: 1000ms 内存限制: 65536kB

描述

Hacker Bill has accidentally lost all the information from his workstation’s hard drive and he has no backup copies of its contents. He does not regret for the loss of the files themselves, but for the very nice and convenient directory structure that he had created and cherished during years of work. Fortunately, Bill has several copies of directory listings from his hard drive. Using those listings he was able to recover full paths (like “WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86”) for some directories. He put all of them in a file by writing each path he has found on a separate line. Your task is to write a program that will help Bill to restore his state of the art directory structure by providing nicely formatted directory tree.

输入

The first line of the input file contains single integer number N (1 <= N <= 500) that denotes a total number of distinct directory paths. Then N lines with directory paths follow. Each directory path occupies a single line and does not contain any spaces, including leading or trailing ones. No path exceeds 80 characters. Each path is listed once and consists of a number of directory names separated by a back slash (“”).

Each directory name consists of 1 to 8 uppercase letters, numbers, or the special characters from the following list: exclamation mark, number sign, dollar sign, percent sign, ampersand, apostrophe, opening and closing parenthesis, hyphen sign, commercial at, circumflex accent, underscore, grave accent, opening and closing curly bracket, and tilde (“!#$%&'()-@^_`{}~”).

输出

Write to the output file the formatted directory tree. Each directory name shall be listed on its own line preceded by a number of spaces that indicate its depth in the directory hierarchy. The subdirectories shall be listed in lexicographic order immediately after their parent directories preceded by one more space than their parent directory. Top level directories shall have no spaces printed before their names and shall be listed in lexicographic order. See sample below for clarification of the output format.

样例输入

7
WINNT\SYSTEM32\CONFIG
GAMES
WINNT\DRIVERS
HOME
WIN\SOFT
GAMES\DRIVERS
WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86

样例输出

GAMES
 DRIVERS
HOME
WIN
 SOFT
WINNT
 DRIVERS
 SYSTEM32
  CERTSRV
   CERTCO~1
    X86
  CONFIG

来源

Northeastern Europe 2000

思路

这道题考察的是类似于字典树的创建。
我们只需在mp中寻找是否存在subS的元素,没有则需创建,有则将p的值设为它所对应的node的地址。
对于输出,样例输出的格式是层序遍历,所以,我们直接使用如下代码。

void outputRes(node *curP, int f) {
	for(auto i: curP->mp) {
		for(int i = 1; i <= f; ++i) {
			cout << " ";
		}
		cout << i.first << endl;
		outputRes(i.second, f+1);
	}
}

注意,空格的数量与层数成 y = x y = x y=x关系,所以在递归传入的数据也要传入层序的值,即变量f

Code

#include <bits/stdc++.h>
using namespace std;

struct node {
    map<string, node*> mp;
} root;

void solve(string s) {
    string subS;
    node *p = &root;
    int j = 0, k = j;
    for(; j < s.size(); ++j) {
        if(s[j] == '\\') {
            subS = s.substr(k, j-k);
            if(p->mp.find(subS) == p->mp.end()) {
                p->mp[subS] = new node;
                p = p->mp[subS];
            } else {
                p = p->mp[subS];
            }
			k = j+1;
        }
    }
    subS = s.substr(k, j-k);
    if(p->mp.find(subS) == p->mp.end()) {
        p->mp[subS] = new node;
        p = p->mp[subS];
    } else {
        p = p->mp[subS];
    }
}

void outputRes(node *curP, int f) {
	for(auto i: curP->mp) {
		for(int i = 1; i <= f; ++i) {
			cout << " ";
		}
		cout << i.first << endl;
		outputRes(i.second, f+1);
	}
}

int main() {
    int N;
    cin >> N;
    for(int i = 1; i <= N; ++i) {
        string s, subS;
        cin >> s;
        solve(s);
    }
	outputRes(&root, 0);
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部