• hihoCoder 1383 : The Book List(书目表)


    hihoCoder #1383 : The Book List(书目表)

    时间限制:1000ms
    单点时限:1000ms
    内存限制:256MB

    Description - 题目描述

    The history of Peking University Library is as long as the history of Peking University. It was build in 1898. At the end of year 2015, it had about 11,000 thousand volumes of books, among which 8,000 thousand volumes were paper books and the others were digital ones. Chairman Mao Zedong worked in Peking University Library for a few months as an assistant during 1918 to 1919. He earned 8 Dayang per month there, while the salary of top professors in Peking University is about 280 Dayang per month.

    Now Han Meimei just takes the position which Chairman Mao used to be in Peking University Library. Her first job is to rearrange a list of books. Every entry in the list is in the format shown below:

    北大图书馆和北大一样历史悠久,建于1898年。在2015年末,共计藏书1100万卷,其间800万为纸质书,其余为电子书。毛主席曾在1918到1919年间担任北大图书管理员数月。当时他月薪8大洋,而北大顶尖教授的月薪为280大洋。
    现在韩梅梅和毛主席一样成为了北大图书管理员。她的首要工作就是重新编排一份书目表。原条目的格式如下:
    CN

    CATEGORY 1/CATEGORY 2/..../CATEGORY n/BOOKNAME

    It means that the book BOOKNAME belongs to CATEGORY n, and CATEGORY n belongs to CATEGORY n-1, and CATEGORY n-1 belongs to CATEGORY n-2...... Each book belongs to some categories. Let's call CATEGORY1  "first class category", and CATEGORY 2 "second class category", ...ect. This is an example:

    其表示图书 BOOKNAME 属于 CATEGORY n, 并且 CATEGORY n 属于 CATEGORY n-1, 以及 CATEGORY n-1 属于 CATEGORY n-2...... 每本书可从属若干类别。我们可以称 CATEGORY1 为"一级大类",CATEGORY 2"二级大类",...以此类推。样例如下:
    CN

    MATH/GRAPH THEORY
    ART/HISTORY/JAPANESE HISTORY/JAPANESE ACIENT HISTORY
    ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON LIUBEI
    ART/HISTORY/CHINESE HISTORY/CHINESE MORDEN HISTORY
    ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON CAOCAO

    Han Meimei needs to make a new list on which the relationship between books and the categories is shown by indents. The rules are:

    1) The n-th class category has an indent of  4×(n-1) spaces before it.
    2) The book directly belongs to the n-th class category has an indent of  4×n spaces before it.
    3) The categories and books which directly belong to a category X should be list below X in dictionary order. But all categories go before all books. 
    4) All first class categories are also list by dictionary order.

    For example, the book list above should be changed into the new list shown below:

    韩梅梅需要制作一份以缩进体现图书与其类别的书目表。规则如下:
    1) 第n级大类的类别名缩进为 4×(n-1) 个空格在前。
    2) 属于第n级大类的图书名缩进为  4×n 个空格在前。
    3) 直属类别X的图书与次级类别需在类别X下方以字典序列出。但是所有次级类别名都要在书名上方。
    4) 所有一级大类的类别名要以字典序列出。
    上述例子的书目表应转化如下:
    CN
    ART
        HISTORY
            CHINESE HISTORY
                THREE KINDOM
                    RESEARCHES ON CAOCAO
                    RESEARCHES ON LIUBEI
                CHINESE MORDEN HISTORY
            JAPANESE HISTORY
                JAPANESE ACIENT HISTORY
    MATH
        GRAPH THEORY
    

    Please help Han Meimei to write a program to deal with her job.

    帮韩梅梅敲个程序搞定这件事吧。
    CN

    Input - 输入

    There are no more than 10 test cases.
    Each case is a list of no more than 30 books, ending by a line of "0". 
    The description of a book contains only uppercase letters, digits, '/' and spaces, and it's no more than 100 characters.
    Please note that, a same book may be listed more than once in the original list, but in the new list, each book only can be listed once. If two books have the same name but belong to different categories, they are different books.

    测试用例不超过10组。
    每个测试用例的书目表的书不超过30本,结束标准为一行"0"。
    每本书的描述之包含大写字母,数字,'/'与空格,并且字符数量不超过100个。
    注意,在原书目表中可能多次出现同一本书,但在新表上,每本书只能录入一次。如果有两本书同名但不同目录,则他们是不同的书。
    CN

     

    Output - 输出

    For each test case, print "Case n:" first(n starts from 1), then print the new list as required.

    对于每个测试用例,先输出"Case n:"(n从1开始),然后再按要求输出新的书目表。
    CN

    Sample Input - 样例输入

    B/A
    B/A
    B/B
    0
    A1/B1/B32/B7
    A1/B/B2/B4/C5
    A1/B1/B2/B6/C5
    A1/B1/B2/B5
    A1/B1/B2/B1
    A1/B3/B2
    A3/B1
    A0/A1
    0
    

    Sample Output - 样例输出

    Case 1:
    B
        A
        B
    Case 2:
    A0
        A1
    A1
        B
            B2
                B4
                    C5
        B1
            B2
                B6
                    C5
                B1
                B5
            B32
                B7
        B3
            B2
    A3
        B1
    

    【题解】

      字典树。DFS就能实现输出。 读取结束条件是"0",只判断第一个的话就会忽略了"0XXXXXXXXXX"。

      输出优先级分别是:当前目录,次级目录,书名,同级目录。

      使用字典树的时候需要分清楚书名和目录名,需要弄好结束标志,不然A和AB只输出了AB。

    【代码 C++】

     1 #include <cstdio>
     2 #include <cstring>
     3 int data[3100][2][200], iD;
     4 char rd[105];
     5 bool build(){
     6     memset(data, -1, sizeof(data)); iD = 0;
     7     bool opt;
     8     int i, j, now, nxt, len;
     9     while (opt = gets(rd)){
    10         if (rd[0] == '0' && rd[1] == 0) break;
    11         for (i = 1; rd[i]; ++i) if (rd[i] == '/') j = i;
    12         len = i; i = now = 0;
    13         do{
    14             nxt = rd[i];
    15             if (data[now][0][nxt] == -1) data[now][0][nxt] = ++iD;
    16             if (i == j) break;
    17             now = data[now][0][nxt];
    18         } while (++i);
    19         if (data[now][1][nxt] == -1) data[now][1][nxt] = ++iD;
    20         for (now = data[now][1][nxt], ++i; i <= len; ++i){
    21             nxt = rd[i];
    22             if (data[now][1][nxt] == -1) data[now][1][nxt] = ++iD;
    23             now = data[now][1][nxt];
    24         }
    25     }
    26     return opt;
    27 }
    28 
    29 char s[3005];
    30 void optS(int c, int L, int R){//[L, R)
    31     c <<= 2;
    32     while (c--) putchar(' ');
    33     while (L < R) putchar(s[L]), ++L;
    34     puts("");
    35 }
    36 void fidB(int now, int c, int L, int R){
    37     int i;
    38     if (~data[now][1][0]) optS(c, L, R);
    39     for (i = 0; i < 200; ++i){
    40         if (data[now][1][i] == -1) continue;
    41         s[R] = i; fidB(data[now][1][i], c, L, R + 1);
    42     }
    43 }
    44 void fidC(int now, int c, int L, int R){
    45     int i;
    46     if (~data[now][0]['/']){
    47         optS(c, L, R);
    48         fidC(data[now][0]['/'], c + 1, R, R);
    49         if (~data[now][1]['/']) fidB(data[now][1]['/'], c + 1, R, R);
    50     }
    51     for (i = 0; i < 200; ++i){
    52         if (data[now][0][i] == -1 || i == '/') continue;
    53         s[R] = i; fidC(data[now][0][i], c, L, R + 1);
    54     }
    55 }
    56 
    57 int main(){
    58     int i = 0;
    59     while (build()){
    60         printf("Case %d:
    ", ++i);
    61         fidC(0, 0, 0, 0);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    【转贴】龙芯生态产品和解决方案巡展(第四篇)——存储
    【转贴】龙芯生态产品和解决方案巡展(第五篇)——云终端
    【转贴】龙芯生态产品和解决方案巡展(第六篇) ——操作系统
    【转贴】龙芯生态产品和解决方案巡展(一)
    【转贴】龙芯生态产品和解决方案巡展(第二篇)——笔记本电脑
    【转贴】龙芯生态产品和解决方案巡展(第三篇)——服务器
    【转贴】我们的龙芯3号---致龙芯15周年
    【转贴】GS464/GS464E
    【转贴】Windows virtio 驱动
    【转贴】Windows常用命令实例
  • 原文地址:https://www.cnblogs.com/Simon-X/p/5910842.html
Copyright © 2020-2023  润新知