UPC-5083 - Hidden Hierarchy - 模拟

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







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.


/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


- / 1009727
- /cerc/ 1009627
  /cerc/documents/ 4000
- /cerc/problems/ 1005627
- /cerc/problems/a/ 1005508
  /cerc/problems/a/testdata/ 1000008
+ /cerc/problems/b/ 119
+ /sys/ 100






#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;
            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];
                cnt[cur_idx] += num, s[j + 1] = ch_s, tmp[pre + 1] = ch_tmp, pre = j, pre_idx = cur_idx;
    scanf("%lld", &t);
    return 0;
版权声明:来自lucien.ink,转载请注明文章出处。 https://blog.csdn.net/xs18952904/article/details/79952370


在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!

  • xujin_chen
  • xujin_chen
  • 2016-12-10 11:52:41
  • 5280


  • 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 - 模拟