zoukankan      html  css  js  c++  java
  • 2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)

    题目传送门

    1383 : The Book List

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

    描述

    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:

    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:

    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:

    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.

    输入

    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.

    输出

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

    样例输入

    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

    样例输出

    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

    字典树大模拟。因为输出要按字典序输出,所以输入的时候需要给字符串排个序。而且输出的时候在相同层有后继的优先输出(题目没说清QAQ),所以在输出上要一点小处理。

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<algorithm>
      5 #define clr(x) memset(x,0,sizeof(x))
      6 #define maxnode 3010
      7 using namespace std;
      8 struct node
      9 {
     10     int lt,rt;
     11     char *str;
     12     int val;
     13 };
     14 struct trie
     15 {
     16     node poi[maxnode];
     17     int nodelen;
     18     int head;
     19     trie() {nodelen=1; head=0; clr(poi);}
     20     void clear() {nodelen=1;head=0; clr(poi);}
     21     void insert(int fa,int now,char *stri)
     22     {
     23 //       printf("fat:%d p:%d nodelen:%d head:%d char:%s
    ",fa,now,nodelen,head,stri);
     24         int end=false,i=0;
     25         while(stri[i] && stri[i]!='/')
     26             i++;
     27         if(stri[i]==0)
     28             end=true;
     29         stri[i]=0;
     30         if(!now)
     31         {
     32             now=newnode(stri);
     33             poi[fa].lt=now;
     34             if(!end)
     35                 insert(now,0,stri+i+1);
     36             else
     37                 poi[now].val++;
     38             return ;
     39         }
     40         int q;
     41         while(now && strcmp(poi[now].str,stri)!=0)
     42         {
     43             q=now;
     44             now=poi[now].rt;
     45         }
     46         if(!now)
     47         {
     48            now=newnode(stri);
     49            poi[q].rt=now;
     50         }
     51         if(!end)
     52         {
     53             insert(now,poi[now].lt,stri+i+1);
     54         }
     55         else
     56         {
     57             poi[now].val++;
     58         }
     59         return ;
     60     }
     61     int newnode(char *stri)
     62     {
     63         if(!head)
     64         {
     65             head=nodelen;
     66         }
     67         poi[nodelen].str=stri;
     68         return nodelen++;
     69     }
     70     void output(int node,int dep)
     71     {
     72         if(node==0)
     73         {
     74             return ;
     75         }
     76         int q=node;
     77         while(q)
     78         {
     79             if(poi[q].lt!=0)
     80             {
     81                 for(int i=0;i<dep;i++)
     82                     printf("    ");
     83                 printf("%s",poi[q].str);
     84                 printf("
    ");
     85                 output(poi[q].lt,dep+1);
     86             }
     87             q=poi[q].rt;
     88         }
     89         q=node;
     90         while(q)
     91         {
     92             if(poi[q].val)
     93             {
     94                 for(int i=0;i<dep;i++)
     95                     printf("    ");
     96                 printf("%s",poi[q].str);
     97                 printf("
    ");
     98             }
     99             q=poi[q].rt;
    100         }
    101      return ;
    102     }
    103 
    104 
    105 }tried;
    106 bool cmp(char *a,char *b)
    107 {
    108     return strcmp(a,b)<0;
    109 }
    110 char s[110],deal[maxnode][110];
    111 char *dir[maxnode];
    112 int main()
    113 {
    114     int n,kase=0;
    115     while(fgets(s,110,stdin)!=NULL)
    116     {
    117         clr(deal);
    118         n=0;
    119         s[strlen(s)-1]='';
    120         tried.clear();
    121         strcpy(deal[n++],s);
    122         dir[0]=deal[0];
    123         while(fgets(s,110,stdin)!=NULL && strcmp(s,"0
    ")!=0)
    124         {
    125             s[strlen(s)-1]='';
    126             strcpy(deal[n],s);
    127             dir[n]=deal[n];
    128             n++;
    129         }
    130         sort(dir+0,dir+n,cmp);
    131         for(int i=0;i<n;i++)
    132         {
    133             tried.insert(0,tried.head,dir[i]);
    134         }
    135         printf("Case %d:
    ",++kase);
    136         tried.output(tried.head,0);
    137     }
    138     return 0;
    139 }
    
    
  • 相关阅读:
    Java8时间处理
    yii2.0上传图片
    将字符串不足长度的填充到指定长度
    通过PHPExcel将Excel表文件中数据导入数据库
    css万能清除原理
    浮动+清除浮动
    DIV滚动条设置添加 CSS滚动条显示与滚动条隐藏
    地图上显示点在点上标注当前点的id
    百度地图点击地址后显示图标,保存到数据库之后,页面显示的是保存的坐标图标
    百度地图API
  • 原文地址:https://www.cnblogs.com/wujiechao/p/5907415.html
Copyright © 2011-2022 走看看