CERC 2017 H:Hidden Hierarchy (模拟)

 Hidden Hierarchy

时间限制: 1 Sec  内存限制: 512 MB
提交: 84  解决: 11
[提交][状态][讨论版][命题人:admin]

题目描述

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 ≤ 106) — 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 ≤ 109) — the threshold directory size.

输出

Output the minimal display of the filesystem hierarchy for the given threshold as described above.

样例输入

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

样例输出

- / 1009727

- /cerc/ 1009627

  /cerc/documents/ 4000

- /cerc/problems/ 1005627

- /cerc/problems/a/ 1005508

  /cerc/problems/a/testdata/ 1000008

+ /cerc/problems/b/ 119

+ /sys/ 100

提示

来源


[题意]

    太长,废话太多,   大概意思是  linux 目录,n条记录, 每条记录代表 一个目录下有个  大小为 val文件

    根目录为 “/”; 

    现在给个 t  问 对于 这个目录 下的所有文件大小 

    如果 大于 t  那么  “-” 展开,

    如果 小于 t  那么“+" 折叠    

    如果是底层目录了,  那么 输出" " 一个空格,  

    后面都要跟着 目录信息,和 目录的大小;

    如果是”-“ ,则进入到子目录中 循环操作,    目录按字典序输出

[思路]

    就是模拟了,节点 存储 用 map 容器 里面价格 set 

    考虑到 超时问题,  一律用 scanf 不用cin cout,    string  转化 char 输出  string 调用 ”.c_str“ 函数

    把串截开, 用char  s  s[i+1] 赋值0 s就是 i+1 前面的内容;

    目录就是一颗数,  dfs 遍历 下去 把 文件大小 计算出来

    然后 第二次 dfs  输出


[code]

   

/*
*Date:4/16/2018
*Tile: CERC 2017  H:Hidden Hierarchy 
*Category: 模拟 dfs
*Attention: 注意截断 , 存储点 把 目录作为 节点
*
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+7;

map<string,set<string> >mp;
map<string,ll> mp_size;
ll t;
void dfs1(string str)
{
    for(auto next_str:mp[str])
    {
        dfs1(next_str);
        mp_size[str]+=mp_size[next_str];
    }
}
void dfs2(string str)
{
    if(mp[str].size()==0)
    {
        printf("  %s %lld\n",str.c_str(),mp_size[str]);// 空
        return ;
    }
    bool flag=true;
    for( auto next_str: mp[str]) // 循环遍历当前目录下的所有目录 看是不是存在子目录 >t
        if(mp_size[next_str]>=t)
            flag=false;
    if(flag)// 没有 直接输出 +
    {
        printf("+ %s %lld\n",str.c_str(),mp_size[str]);
    }
    else// 又有的话 下一层
    {
        printf("- %s %lld\n",str.c_str(),mp_size[str]);
        for(auto next_str:mp[str])
            dfs2(next_str);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    mp_size.clear();
    for(int i=1;i<=n;i++)
    {
        char s[MAXN];
        ll val;
        scanf("%s%lld",s,&val);
        string fa="//";
        for(int i=0;s[i];i++)
        {
            if(s[i]=='/')
            {
                char temp=s[i+1];
                s[i+1]=0;
                mp[fa].insert(s);
                fa=s;
                s[i+1]=temp;
            }
        }
        mp_size[fa]+=val;
    }
    scanf("%lld",&t);
    dfs1("//");
    dfs2( *(mp["//"].begin()) );
    return 0;
}
/*
2
/a/b.c 100
/e/c 2
1000
*/

    

123 

posted @ 2018-04-16 20:58  Sizaif  阅读(293)  评论(0编辑  收藏  举报