当前位置: 首页 > article >正文

OpenJudge | Disk Tree

总时间限制: 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);
}

http://www.kler.cn/news/340502.html

相关文章:

  • vue 条件渲染
  • Python的pandas库基本操作(数据分析)
  • 【LeetCode: 344. 反转字符串 | 双指针模拟】
  • Golang 进阶1 —— 面向对象
  • 图的最短路径算法
  • threads_created增加过大?
  • TLS 加密的原理和过程
  • C++实现字符串 trim,C++实现字符串split, C++如何分割字符串为数组,C++如何去除字符串两边的空格
  • (笔记)第三期书生·浦语大模型实战营(十一卷王场)–书生基础岛第3关---浦语提示词工程实践
  • 如何使用pymysql和psycopg2执行SQL语句
  • 使用XML实现MyBatis的基础操作
  • pandas的用法
  • Github界面学习
  • C++ 函数重载
  • 手动更换SSL证书教程及注意事项
  • 【论文阅读】AUTOREGRESSIVE ACTION SEQUENCE LEARNING FOR ROBOTIC MANIPULATION
  • 接着上一篇stp 实验继续
  • Http 协议和 RPC 协议有什么区别?
  • OpenAI .NET 库稳定版发布,支持 GPT-4o 并改进 API 功能
  • 逼近理论及应用精解【9】