题目传送门

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),所以在输出上要一点小处理。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define maxnode 3010
using namespace std;
struct node
{
int lt,rt;
char *str;
int val;
};
struct trie
{
node poi[maxnode];
int nodelen;
int head;
trie() {nodelen=; head=; clr(poi);}
void clear() {nodelen=;head=; clr(poi);}
void insert(int fa,int now,char *stri)
{
// printf("fat:%d p:%d nodelen:%d head:%d char:%s\n",fa,now,nodelen,head,stri);
int end=false,i=;
while(stri[i] && stri[i]!='/')
i++;
if(stri[i]==)
end=true;
stri[i]=;
if(!now)
{
now=newnode(stri);
poi[fa].lt=now;
if(!end)
insert(now,,stri+i+);
else
poi[now].val++;
return ;
}
int q;
while(now && strcmp(poi[now].str,stri)!=)
{
q=now;
now=poi[now].rt;
}
if(!now)
{
now=newnode(stri);
poi[q].rt=now;
}
if(!end)
{
insert(now,poi[now].lt,stri+i+);
}
else
{
poi[now].val++;
}
return ;
}
int newnode(char *stri)
{
if(!head)
{
head=nodelen;
}
poi[nodelen].str=stri;
return nodelen++;
}
void output(int node,int dep)
{
if(node==)
{
return ;
}
int q=node;
while(q)
{
if(poi[q].lt!=)
{
for(int i=;i<dep;i++)
printf(" ");
printf("%s",poi[q].str);
printf("\n");
output(poi[q].lt,dep+);
}
q=poi[q].rt;
}
q=node;
while(q)
{
if(poi[q].val)
{
for(int i=;i<dep;i++)
printf(" ");
printf("%s",poi[q].str);
printf("\n");
}
q=poi[q].rt;
}
return ;
} }tried;
bool cmp(char *a,char *b)
{
return strcmp(a,b)<;
}
char s[],deal[maxnode][];
char *dir[maxnode];
int main()
{
int n,kase=;
while(fgets(s,,stdin)!=NULL)
{
clr(deal);
n=;
s[strlen(s)-]='\0';
tried.clear();
strcpy(deal[n++],s);
dir[]=deal[];
while(fgets(s,,stdin)!=NULL && strcmp(s,"0\n")!=)
{
s[strlen(s)-]='\0';
strcpy(deal[n],s);
dir[n]=deal[n];
n++;
}
sort(dir+,dir+n,cmp);
for(int i=;i<n;i++)
{
tried.insert(,tried.head,dir[i]);
}
printf("Case %d:\n",++kase);
tried.output(tried.head,);
}
return ;
}

2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)的更多相关文章

  1. 【icpc网络赛大连赛区】Sparse Graph

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...

  2. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

  3. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  4. 2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组

    2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组 [Problem Description] ​ 给你一个\([1,n]\)的排列,查询\([l,r]\)区间内有多少对 ...

  5. 【2018ACM/ICPC网络赛】沈阳赛区

    这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K  Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...

  6. Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

    Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...

  7. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  8. 南京网络赛I-Skr【回文树模板】

    19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...

  9. ACM-ICPC2018沈阳网络赛 Lattice's basics in digital electronics(模拟)

    Lattice's basics in digital electronics 44.08% 1000ms 131072K   LATTICE is learning Digital Electron ...

随机推荐

  1. 【洛谷 P1390】 公约数的和 (欧拉函数)

    题目链接 做过\(n\)遍这种题了... 答案就是\(\sum_{i=1}^{n}\sum_{j=1}^{n/i}[\varphi(j)*i]\) 线筛欧拉函数求前缀和直接算就行. #include ...

  2. NYOJ 1237 最大岛屿 (深搜)

    题目链接 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王.  这是一个由海洋. ...

  3. js中三种定义变量 const, var, let 的区别

    js中三种定义变量的方式const, var, let的区别 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 ...

  4. bzoj 2809 左偏树\平衡树启发式合并

    首先我们对于一颗树,要选取最多的节点使得代价和不超过m,那么我们可以对于每一个节点维护一个平衡树,平衡树维护代价以及代价的和,那么我们可以在logn的时间内求出这个子树最多选取的节点数,然后对于一个节 ...

  5. bootstrap基本用法

    进入中文官网:http://www.bootcss.com   开始第一个Demo   准备工作: (1)进入bootstrap中文官网,点击起步 (2)下载生产环境         下载好的文件是一 ...

  6. “adb server is out of date.

    今天,久未出现的著名的“adb server is out of date.  killing”又发生了,在此,将解决方法记下,以便日后查看. 1. 错误信息: C:\Users\lizy>ad ...

  7. python中正则用法举例

    一.根据正则表达式替换字符串 import re text='abc123' text=re.sub(r'\d','-',text) print(text) 输出:abc---将每个数字替换为-,如果 ...

  8. 74cms 注入exp

    遇到就瞎写了一个: #!/usr/bin/env python #encoding:utf-8 #by i3ekr import requests,optparse,re parse = optpar ...

  9. jq 浏览器窗口大小发生变化时

    当调整浏览器窗口的大小时,发生 resize 事件: $(selector).resize(); 实例 对浏览器窗口调整大小进行计数: $(window).resize(function() { $( ...

  10. PHP路由代码

    <?php /**  * 路由  * @author 角度 QQ:1286522207  *  */ class Dispatcher extends Action {     private ...