UPC-5083 - Hidden Hierarchy - 模拟

原创 2018年04月15日 19:53:48

题解链接:

https://lucien.ink/archives/154/


题目链接:

http://exam.upc.edu.cn/problem.php?id=5083


题目:

题目描述

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

题意:

  给你n个文件的地址,让你从根目录开始展开文件夹直到这个文件夹里面所有文件的大小小于所给的阈值t或者是没有子目录可以展开。


思路:

  把所有路径都哈希一下,建一颗树,然后前序遍历即可,对于同一层的节点需要按照节点的字典序从小到大输出。


实现:

#include <bits/stdc++.h>
const int maxn = int(5e4) + 7;
int pre, cur_idx, pre_idx, n, tot = -1;
long long num, t, cnt[maxn];
char path[maxn][107] = {"/"};
std::map<std::string, int> idx;
struct Node {
    int index;
    bool operator < (const Node &tmp) const { return strcmp(path[index], path[tmp.index]) < 0; }
};
std::map<int, std::set<Node>> tree;
void dfs(int u) {
    if (tree[u].empty()) printf("  %s %lld\n", path[u], cnt[u]);
    else {
        if (cnt[u] >= t) {
            bool flag = false;
            for (auto i : tree[u])
                if (cnt[i.index] >= t) {
                    flag = true;
                    break;
                }
            if (flag) {
                printf("- %s %lld\n", path[u], cnt[u]);
                for (auto i : tree[u]) dfs(i.index);
            } else printf("+ %s %lld\n", path[u], cnt[u]);
        } else printf("+ %s %lld\n", path[u], cnt[u]);
    }
}

int main() {
//    freopen("in.txt", "r", stdin);
    char s[1007], tmp[1007], ch_tmp, ch_s;
    idx[path[0]] = ++tot;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf(" %s %lld", s, &num);
        strcpy(tmp, s);
        pre_idx = pre = 0;
        cnt[0] += num;
        for (int j = 1; s[j]; j++) {
            if (s[j] == '/') {
                ch_tmp = tmp[pre + 1], ch_s = s[j + 1];
                tmp[pre + 1] = s[j + 1] = '\0';
                if (!idx.count(s)) {
                    cur_idx = idx[s] = ++tot;
                    strcpy(path[tot], s);
                } else cur_idx = idx[s];
                tree[pre_idx].insert({cur_idx});
                cnt[cur_idx] += num, s[j + 1] = ch_s, tmp[pre + 1] = ch_tmp, pre = j, pre_idx = cur_idx;
            }
        }
    }
    scanf("%lld", &t);
    dfs(0);
    return 0;
}
版权声明:来自lucien.ink,转载请注明文章出处。 https://blog.csdn.net/xs18952904/article/details/79952370

ios中的视图调试(ios8苹果引入了强大的新技术,不可不看)

在ios8以前,我们想要进行ui实时调试,可以依赖的技术手段有:  用xcode启动app,然后点击xcode暂停运行按钮,然后在调试控制台输入下面语句: po [[UIWindow keyWin...
  • openglnewbee
  • openglnewbee
  • 2014-12-27 18:08:26
  • 8080

CERC 2017 H:Hidden Hierarchy (模拟)

 Hidden Hierarchy时间限制: 1 Sec  内存限制: 512 MB提交: 84  解决: 11[提交][状态][讨论版][命题人:admin]题目描述You are working ...
  • sizaif
  • sizaif
  • 2018-04-16 20:58:25
  • 17

解决net.sf.json.JSONException: There is a cycle in the hierarchy!

我们把一个对象或者集合转化成一个json字符串的时候,他会遍历每一个字段,当你对象A里面有对象B,独享B里面有对象A的时候,这时候转化的时候就会抛出net.sf.json.JSONException:...
  • xujin_chen
  • xujin_chen
  • 2016-12-10 11:52:41
  • 5280

自动化测试页面上对hidden控件的处理

我们在进行自动化测试时,经常需要对页面的元素进行定位取值。如果该元素是enable的可以很方便定位。但是有时需要定位hidden的元素,直接定位就成了问题。如果通过firebug或者其他工具发现是hi...
  • leejeff
  • leejeff
  • 2015-07-08 12:39:13
  • 1963

解决The hierarchy of the type is inconsistent错误

解决The hierarchy of the type is inconsistent错误,自己的类继承于某个类,这个类或者这个类继承的类或者再往上继承的某个类所在的jar包没有被引入。...
  • u011983531
  • u011983531
  • 2015-08-21 10:40:55
  • 10993

使用RecyclerView CardView相关问题 解决The hierarchy of the type is inconsistent问题

在使用RecyclerView CardView(参见博客点击打开链接)时会出现The hierarchy of the type is inconsistent,查询后解决方案如下: I found...
  • u013439635
  • u013439635
  • 2015-10-23 00:50:05
  • 1313

The hierarchy of the type is inconsistent

编写java程序时,出现The hierarchy of the type is inconsistent 错误。   可能的原因:自己的类继承于某个类,这个类或者这个类继承的类或者再往上继承...
  • ye1992
  • ye1992
  • 2014-09-27 17:48:10
  • 10608

The hierarchy of the type XXX is inconsistent异常排查方式

最近在学习使用shiro框架在学习过程中由于要自定义一个权限认证拦截器当继承AuthorizationFilter类时报出了“The hierarchy of the type XXX is inco...
  • guying4875
  • guying4875
  • 2016-12-14 09:05:13
  • 3321

问题解决 The hierarchy of the type is inconsistent

由于我在myeclipse里建了两个JAVA PROJECT项目,分别是A projiect和B projiect,项目A引用了一些JAR包,然后项目B引用了项目A,但是B没有引用A的JAR包,就出现...
  • zhq426
  • zhq426
  • 2012-10-30 12:49:37
  • 98941

The hierarchy of the type EditLocationAction is inconsistent 错误原因之一

这种错误可能有多种原因。其中一种原因是:当前类A继承了某类B,而类B是由某C.jar所引入的,如果C.jar比原始项目的C.jar版本新,则会出现The hierarchy of the type E...
  • ljg888
  • ljg888
  • 2013-12-16 16:15:13
  • 4507
收藏助手
不良信息举报
您举报文章:UPC-5083 - Hidden Hierarchy - 模拟
举报原因:
原因补充:

(最多只允许输入30个字)