时间限制: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:

ARTHISTORYCHINESE HISTORYTHREE KINDOMRESEARCHES ON CAOCAORESEARCHES ON LIUBEICHINESE MORDEN HISTORYJAPANESE HISTORYJAPANESE ACIENT HISTORY
MATHGRAPH 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:
BAB
Case 2:
A0A1
A1BB2B4C5B1B2B6C5B1B5B32B7B3B2
A3B1

自己做的一小撮测试用例(大小写不敏感),查看纯文本view plain,不然格式不对

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
A/B/A/B
A/B/A/B
0
A/B
A/C
0
A
B
C
D
0
A
0
A1/B1/B1
A1/B1/B2
A1/B2/B1
A1/B3
A1/B4
B5
B6
C1/C1
C1/C2
C1/C3
0
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON CAOCAO
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON LIUBEI
ART/HISTORY/CHINESE HISTORY/CHINESE MORDEN HISTORY
ART/HISTORY/JAPANESE HISTORY/JAPANESE ACIENT HISTORY
MATH/GRAPH THEORY
0
a b/b c/d e
d e
a b/d e
0
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
0
0/0/0/0
0/0/0/1
0
1/2/3/4
1/2/3/2
1/2/3/2
0/ / / / / /a/ /
0/
0
0000/0000/ /0000
0 0 0/0000/ /0000
0 0 0/0000/0/0000
0
a/b/c
a/a
b/a
b/b
b/c/d
b/e/f/g
0/a
0
A/B/C
A/B
0

output:

Case 1:
BAB
Case 2:
A0A1
A1BB2B4C5B1B2B6C5B1B5B32B7B3B2
A3B1
Case 3:
ABAB
Case 4:
ABC
Case 5:
A
B
C
D
Case 6:
A
Case 7:
A1B1B1B2B2B1B3B4
C1C1C2C3
B5
B6
Case 8:
ARTHISTORYCHINESE HISTORYTHREE KINDOMRESEARCHES ON CAOCAORESEARCHES ON LIUBEICHINESE MORDEN HISTORYJAPANESE HISTORYJAPANESE ACIENT HISTORY
MATHGRAPH THEORY
Case 9:
a bb cd ed e
d e
Case 10:
ARTHISTORYCHINESE HISTORYTHREE KINDOMRESEARCHES ON CAOCAORESEARCHES ON LIUBEICHINESE MORDEN HISTORYJAPANESE HISTORYJAPANESE ACIENT HISTORY
MATHGRAPH THEORY
Case 11:
00001
Case 12:
12324
Case 13:a0Case 14:
0 0 00000000000000
000000000000
Case 15:
0a
abca
bcdefgab
Case 16:
ABCB

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <istream>
#include <cstdlib>using namespace std;const int maxnode = 300000, maxdep = 100000;
char a[maxdep + 5];
int lena;
map<string, int> ch[maxnode + 5][2];   //ch[父节点号][是不是叶子节点][节点名称] --> 子节点编号
map<int, string> m; //m[节点编号] --> 节点名称
int val[maxnode + 5]; //叶子节点标记struct Trie {int sz;Trie() {sz = 1;for (int i = 0; i < maxdep + 3; i++) {ch[i][0].clear();ch[i][1].clear();}m.clear();memset(val, 0, sizeof(val));}void clear() {sz = 1;for (int i = 0; i < maxdep + 3; i++) {ch[i][0].clear();ch[i][1].clear();}m.clear();memset(val, 0, sizeof(val));}void insert(char *s, int p) {int in, u = 0;while (1) {lena = 0;for (in = p; s[in] != '/' && s[in] != 0; in++) {a[lena++] = s[in];}a[lena] = 0;string ts = a;if (!ch[u][0][ts] && s[in] != 0) {ch[u][0][ts] = sz++;m[ch[u][0][ts]] = ts; //map from node id to string}if (!ch[u][1][ts] && s[in] == 0) {ch[u][1][ts] = sz++;m[ch[u][1][ts]] = ts; //map from node id to string}//debug// if (!ch[u][0][ts] && !ch[u][1][ts]) {//     if (s[in] == 0) {//       ch[u][1][ts] = sz++;//       m[ch[u][1][ts]] = ts; //map from node id to string//   }//     else {//        ch[u][0][ts] = sz++;//       m[ch[u][0][ts]] = ts; //map from node id to string//   }// }u = (s[in] ? ch[u][0][ts] : ch[u][1][ts]);if (s[in] == 0) {val[u] = 1;break;}p = in + 1;}}void output(int u, int dep) {//先输出目录for (map<string, int>::iterator i = ch[u][0].begin(); i != ch[u][0].end(); i++) {if (i->second != 0) {for (int j = 0; j < dep; j++) {printf("    ");}cout << m[i->second] << endl;output(i->second, dep + 1);}}//再输出书for (map<string, int>::iterator i = ch[u][1].begin(); i != ch[u][1].end(); i++) {if (val[i->second]) {for (int j = 0; j < dep; j++) {printf("    ");}cout << m[i->second] << endl;}}}
};char s[maxnode + 5];int main()
{/*freopen("D:\\input.txt", "r", stdin);freopen("D:\\output.txt", "w", stdout);*/Trie tree;int kase = 0;while (gets(s) != NULL) {if (s[0] == '0' && s[1] == '\0') {printf("Case %d:\n", ++kase);tree.output(0, 0);tree.clear();continue;}else {tree.insert(s, 0);}}return 0;
}

hihocoder1383 The Book List 字典树相关推荐

  1. hdu5296 01字典树

    根据二进制建一棵01字典树,每个节点的答案等于左节点0的个数 * 右节点1的个数 * 2,遍历整棵树就能得到答案. AC代码: #include<cstdio> using namespa ...

  2. BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...

  3. 2014百度之星 Xor Sum(字典树+贪心)

    题目在HDU_OJ Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometh ...

  4. HDU6964 I love counting (字典树+莫队)

    题意: 给定一个长度为 nnn 的序列c,qc,qc,q 次询问,每次给出l,r,a,bl,r,a,bl,r,a,b求在[l,r][l,r][l,r]中有多少种不同的值 kkk 满足 k⊕a≤b​.k ...

  5. Beautiful Subarrays (01字典树 瞎搞)

    题意: 题解: 一看问的是子序列,并且还是异或. 首先想到01字典树,再一看让你求子序列的个数,大致是想让你把这个序列进行前缀异或处理后然后再01字典树上进行操作吧. 假设01字典树往左边是0右边是1 ...

  6. CF888G Xor-MST (01字典树+MST)

    题意: 给你n个数,每个数有一个值. 问你这n个数的最小生成树为多少,两点之间的边权为异或值. 题解: 参考了洛谷上的一个题解,总觉得这样的时间复杂度会爆炸,但是确确实实没爆炸. 我们每次去合并两个点 ...

  7. Perfect Security (01字典树删除点)

    题意: 第一行给你一个n 第二行给你n个数字 分别是a[1],a[2]-a[n]. 第三行给你n个数字 分别是b[1],b[2]-b[n]. 问:第三行的序列可自由排列,要求排列之后的顺序 a[1] ...

  8. Vitya and Strange Lesson (01字典树)

    题意:给定一组数,然后对所有的数进行异或操作m次异或操作,问你每次操作后,没有出现过的最小的非负整数 题解: 首先需要一个性质(ab)c=a(bc); 所以我们没有必要对于所有的数进行异或操作. 我们 ...

  9. 奶牛异或(01字典树)

    题意: 让你找一个连续区间异或和最大,如果有相同的,则输出断点较小的. 题解: 01字典树,利用前缀和的思想进行求解,我们在插入前缀的同时,也在不断的更新最大值. 我们查询当前 二进制字符串与已经插入 ...

最新文章

  1. MySQL 中 MyISAM 中的查询为什么比 InnoDB 快?
  2. html for 循环模板
  3. python回收机制
  4. 201521123044 《Java程序设计》第01周学习总结
  5. 【原创】OllyDBG 入门系列(五)-消息断点及 RUN 跟踪
  6. 遇到一个php的错误,php初学者常见的几个错误及解决方法
  7. 使用Asp.Net MVC开发兼职文章系统
  8. [POJ2559POJ3494] Largest Rectangle in a HistogramLargest Submatrix of All 1’s 「单调栈」
  9. 操作系统实验1—基于优先数的时间片轮转调度算法调度处理模拟程序设计
  10. 用python做名片管理器_Python简单的名片管理器
  11. Python调用R出现“UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb2” 问题
  12. 阿龙学堂-hdfs存储数据倾斜
  13. Linuxshell脚本之循环语句
  14. 目前智能手机 微型计算机,微型计算机基础知识1(新).ppt.ppt
  15. 苏州和上海在生活和工作方面的比较
  16. macOS启动U盘制作命令
  17. idea Java 读取MacOS 本地的 rtf 文件内容:中英文皆可
  18. 什么是epub格式,windows上有什么好用的epub阅读器
  19. cydia崩溃如何解决,清理cydia的方法!
  20. 手把手教你绩效管理体系-OKR

热门文章

  1. Go Web 编程 PDF
  2. matlab如何导入大量的图片_本期介绍:如何在论文中插入高清的图片
  3. 华为路ws5200设置虚拟服务器,华为路由器WS5200如何设置上网 最详细的华为路由器WS5200上网设置方法教程...
  4. 吉米_王:浅谈链表和顺序表
  5. 猿创征文|OneOS 万耦启物TB6612驱动电机
  6. GPS nmealib学习笔记 .
  7. 鲁兴海:英国皇室裁缝合作伙伴--地方--人民网
  8. Dockerfile 定制 Rabbitmq 集成延时队列的镜像
  9. 一文看懂 DDD(领域驱动设计)、CQRS和Event Souring与分层架构
  10. wifi 验证加密 总结