Gym - 101620H_Hidden Hierarchy(树+模拟)
Hidden Hierarchy
题目描述
You are working on the user interface for a simple text-based file explorer. One of your tasks is to build a navigation pane displaying the directory hierarchy. As usual, the filesystem consists of directories which may contain files and other directories, which may, in turn, again contain files and other directories etc. Hence, the directories form a hierarchical tree structure. The top-most directory in the hierarchy is called the root directory. If directory d directly contains directory e we will say that d is the parent directory of e while e is a subdirectory od d. Each file has a size expressed in bytes. The directory size is simply the total size of all files directly or indirectly contained inside that directory.
All files and all directories except the root directory have a name — a string that always starts with a letter and consists of only lowercase letters and “.” (dot) characters. All items (files and directories) directly inside the same parent directory must have unique names. Each item (file and directory) can be uniquely described by its path — a string built according to the following rules:
- Path of the root directory is simply “/” (forward slash).
- For a directory d, its path is obtained by concatenating the directory names top to bottom along the hierarchy from the root directory to d, preceding each name with the “/” character and placing another “/” character at the end of the path.
- For a file f , its path is the concatenation of the parent directory path and the name of file f .
We display the directory hierarchy by printing the root directory. We print a directory d by outputting a line of the form “md pd sd” where pd and sd are the path and size of directory d respectively, while md is its expansion marker explained shortly. If d contains other directories we must choose either to collapse it or to expand it. If we choose to expand d we print (using the same rules) all of its subdirectories in lexicographical order by name. If we choose to collapse directory d, we simply ignore its contents.
The expansion marker md is a single blank character when d does not have any subdirectories, “+” (plus) character when we choose to collapse d or a “-” (minus) character when we choose expand d.
Given a list of files in the filesystem and a threshold integer t, display the directory hierarchy ensuring that each directory of size at least t is printed. Additionally, the total number of directories printed should be minimal. Assume there are no empty directories in the filesystem — the entire hierarchy can be deduced from the provided file paths. Note that the root directory has to be printed regardless of its size. Also note that a directory of size at least t only has to be printed, but not necessarily expanded.
输入
The first line contains an integer n (1 ≤ n ≤ 1 000) — the number of files. Each of the following n lines contains a string f and an integer s (1 ≤ s ≤ 1e6) — the path and the size of a single file. Each path is at most 100 characters long and is a valid file path according to the rules above. All paths will be different.
The following line contains an integer t (1 ≤ t ≤ 1e9) — the threshold directory size.
输出
Output the minimal display of the filesystem hierarchy for the given threshold as described above.
样例
input
9
/sys/kernel/notes 100
/cerc/problems/a/testdata/in 1000000
/cerc/problems/a/testdata/out 8
/cerc/problems/a/luka.cc 500
/cerc/problems/a/zuza.cc 5000
/cerc/problems/b/testdata/in 15
/cerc/problems/b/testdata/out 4
/cerc/problems/b/kale.cc 100
/cerc/documents/rules.pdf 4000
10000
output
- / 1009727
- /cerc/ 1009627
/cerc/documents/ 4000
- /cerc/problems/ 1005627
- /cerc/problems/a/ 1005508
/cerc/problems/a/testdata/ 1000008
+ /cerc/problems/b/ 119
+ /sys/ 100
input
8
/b/test/in.a 100
/b/test/in.b 1
/c/test/in.a 100
/c/test/in.b 1
/c/test/pic/in.a.svg 10
/c/test/pic/in.b.svg 10
/a/test/in.a 99
/a/test/in.b 1
101
output
- / 322
+ /a/ 100
- /b/ 101
/b/test/ 101
- /c/ 121
+ /c/test/ 121
input
2
/a/a/a 100
/b.txt 99
200
output
+ / 199
题意
这个题目超级长,废话也超级多。大概就是做一个目录。
有n条路径,每条路径下有一个大小为val的文件。
根文件为“/”;
给一个t;
如果目录下文件大小有一个大于等于t,展开“-”;
如果都小于t,折叠“+”;
如果没有文件,则输出一个“ ”。
注意:输出路径按字典序排序。
思路
首先把每个文件路径及其大小读入,按字典序排序。
然后把路径分开,建树。
根据题目要求输出就可以了。
有几个点注意:
内存问题,刚开始用数组存储,结果导致RE跟MLE二选一,后来改成了vector。
输出问题,这里需要采用递归输出。
剩下的就是疯狂模拟就可以了。
附上AC代码。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
struct WJ//记录文件,及其目录下有多少文件;
{
int len,w,parent;
char name[105];
vector<int>next;
};
struct Char//记录输入的字符串;
{
char s[105];
int w;
};
vector<WJ>p;
char name[105][105];
int num,w,num2;//num,文件个数;num2,每个路径的文件个数。
bool cmp(Char a,Char b)
{
if(strcmp(a.s,b.s)>0)
return false;
else if(strcmp(a.s,b.s)==0)
return a.w<b.w;
return true;
}
void build(int x,int w,int j)
{
p[x].w += w;
if(j>=num2-1)//递归到路径文件就可以退出了;
return;
int i,a;
for(i=0;i<p[x].len;i++)
{
a = p[x].next[i];
if(strcmp(p[a].name,name[j])==0)
{
build(a,w,j+1);
return;
}
}
WJ q;
q.len = q.w = 0;
q.parent = x;
strcpy(q.name,name[j]);
p[x].next.push_back(num++);
p[x].len++;
p.push_back(q);
build(num-1,w,j+1);
}
void print(int x)//递归输出文件路径
{
if(p[x].parent!=-1)
print(p[x].parent);
if(p[x].parent==-1)
return;
printf("%s/",p[x].name);
}
void show(int x)
{
int i,a;
if(p[x].len==0)
{
printf(" /");
print(x);
printf(" %d\n",p[x].w);
return;
}
for(i=0;i<p[x].len;i++)
{
a = p[x].next[i];
if(p[a].w>=w)
break;
}
if(i==p[x].len)
{
printf("+ /");
print(x);
printf(" %d\n",p[x].w);
}
else
{
printf("- /");
print(x);
printf(" %d\n",p[x].w);
for(i=0;i<p[x].len;i++)
show(p[x].next[i]);
}
}
int main()
{
int n,i,j,k,len;
Char s[1050];
WJ q;
q.len = q.w = 0;
q.parent = -1;
p.push_back(q);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%d",s[i].s,&s[i].w);
scanf("%d",&w);
sort(s,s+n,cmp);
num = 1;
for(k=0;k<n;k++)//把每个路径分割;
{
num2 = 0;
len = strlen(s[k].s);
for(i=0;i<len;i++)
{
if(s[k].s[i]=='/')
{
for(j=i+1;s[k].s[j]!='/'&&j<len;j++)
name[num2][j-i-1] = s[k].s[j];
name[num2][j-i-1] = '\0';
num2++;
}
i = j - 1;
}
build(0,s[k].w,0);
}
show(0);
return 0;
}
Gym - 101620H_Hidden Hierarchy(树+模拟)的更多相关文章
- CERC2017 H Hidden Hierarchy(树+模拟)
题意: 在一些给定的目录里按要求展开到制定大小并按字典序输出 思路: 因为有目录这个东西,所以想到模拟一个类似字典树的东西,不过这里每个儿子可能有n个节点,而且不能O(1)查询了 代码超长.. #in ...
- hdu_5818_Joint Stacks(线段树模拟)
题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度 ...
- 【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流
昨天考试被教育了一波.为了学习一下\(T3\)的科技,我就找到了这个远古时期的\(cf\)题(虽然最后\(T3\)还是不会写吧\(QAQ\)) 顾名思义,这个题目其实可以建成一个费用流的模型.我们用流 ...
- hdu3436 splaytree树模拟队列+离散化缩点
数据较大,需要先把每个top不会操作到的段缩成一个点,记录其开始和结束的位置,和top能操作到的点一起建立一颗伸展树模拟 然后就是普通的队列模拟操作 /* 不会被top操作到的区间就缩点 通过spla ...
- poj2828 伸展树模拟
用伸展树模拟插队比线段树快乐3倍.. 但是pojT了.别的oj可以过,直接贴代码. 每次更新时,找到第pos个人,splay到根,然后作为新root的左子树即可 #include<iostrea ...
- C/C++深度优先搜索(递归树模拟)
//C++深度优先搜索(递归树模拟) #define _CRT_SECURE_NO_WARNINGS #include <iostream> #define MAX_N 1000 usin ...
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- POJ-2886 Who Gets the Most Candies?(线段树+模拟)
题目大意:n个小孩按顺时针站成一圈,每次会有一个小孩出队(第一个出队的小孩已知),在他出队时会指定下一个出队的小孩,直到所有的小孩全部出队游戏结束.第p个出队的小孩会得到f(p)个糖果,f(p)为p的 ...
- 【BZOJ4523】[Cqoi2016]路由表 Trie树模拟
[BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组 ...
随机推荐
- Docx 生成word文档
1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...
- Ubuntu中安装gdal python版本
安装过程: python包是从C++包中编译出来的,所以需要将源码下载进行编译安装 1.GDAL中的矢量数据处理OGR依赖于Geos,在安装GDAL之前要安装Geos Geos的下载地址:http:/ ...
- 引入样式表(css)的四种方式
一.使用style属性: 将style属性直接加在html标签里. <标签 style="属性1: 设定值1; 属性2: 设定值2; "> 例如: <td sty ...
- 易语言调用外部DLL详细实例教程
一.准备工作 一.工具:易语言 二.准备一个DLL 1)打开易语言-新建一个Windows动态链接库 2)然后右键新建一个子程序或者用快捷键:Ctrl+N .然后写上代码.我这里写一个 2个字符串拼接 ...
- sqlserver 2008 R2 安装教程(心得记录)
在这里简单的记录下自己安装sqlserver的过程吧(本人以前安装失败过,然后卸载了,就一直没用,现在由于工具原因,重新安装,过程相对第一次安装会复杂点) 1.首先,把以前安装的注册表的对应c盘的文件 ...
- jsp中生成的验证码和存在session里面的验证码不一致的处理
今天在调试项目的时候发现,在提交表单的时候的验证码有问题,问题是这样的:就是通过debug模式查看得知:jsp页面生成的验证码和表单输入的页面输入的一样,但是到后台执行的时候,你会发现他们是不一样的, ...
- 学习JDK1.8集合源码之--WeakHashMap
1. WeakHashMap简介 WeakHashMap继承自AbstractMap,实现了Map接口. 和HashMap一样,WeakHashMap也是一种以key-value键值对的形式进行数据的 ...
- POJ2182Lost Cows
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 7690 Descri ...
- win10 请求操作需要提升解决方案
记录一下: win10 系统管理员 打开后缀为 .xxx 的文件时, 系统提示: 请求操作需要提升 网上搜索了一下,原因是权限不够,故系统提示. 给当前用户加入了 管理员权限,各种权限都无效. ...
- Least Common Multiple (最小公倍数,先除再乘)
思路: 求第一个和第二个元素的最小公倍数,然后拿求得的最小公倍数和第三个元素求最小公倍数,继续下去,直到没有元素 注意:通过最大公约数求最小公倍数的时候,先除再乘,避免溢出 #include ...