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

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

样例输出

GAMESDRIVERS
HOME
WINSOFT
WINNTDRIVERSSYSTEM32CERTSRVCERTCO~1X86CONFIG

来源

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.mrgr.cn/news/46561.html

相关文章:

  • 【 PID 算法 】PID 算法基础
  • 大模型训练_硬件微调知识增强
  • 如何通过openssl生成.crt和.key
  • FPGA工程师成长四阶段
  • 【已解决】git clone报错:Failed to connect to github.com port 443: Timed out
  • 一文通透OpenVLA及其源码剖析——基于Prismatic VLM(SigLIP、DinoV2、Llama 2)及离散化动作预测
  • vue 条件渲染
  • UI开发:从实践到探索
  • YOLO v1详解解读
  • windows中使用类似tree的功能
  • 论文精读:基于概率教师学习的跨域自适应目标检测(ICML2022)
  • yolov11人物背景扣除
  • USB转多路RS485应用-组态软件调试
  • Java基础常见面试题总结(1-2)
  • 04. prometheus 监控 Windows 服务器
  • 架构设计笔记-7-系统架构设计基础知识
  • 【SQL】深入探索SQL调优:提升数据库性能的全面指南
  • 5.toString()、构造方法、垃圾回收、静态变量与静态方法、单例设计模式、内部类
  • 以openai的gpt3 5为例的大模型流式输出实现(原始、Sanic、Flask)- 附免费的key
  • 【QT Quick】页面布局:手动定位与坐标系转换
  • python .pyc是什么文件
  • Java之HashMap详解
  • 使用 favicon MD5 值检测网站框架
  • 内存泄露和内存溢出案例解析
  • jenkins远程调用
  • 基于Qt/QChart实现折线图和散点图的绘制示例程序解析