-
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
描述
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.
自己做的一小撮测试用例(大小写不敏感),查看纯文本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:
B
A
B
Case 2:
A0
A1
A1
B
B2
B4
C5
B1
B2
B6
C5
B1
B5
B32
B7
B3
B2
A3
B1
Case 3:
A
B
A
B
Case 4:
A
B
C
Case 5:
A
B
C
D
Case 6:
A
Case 7:
A1
B1
B1
B2
B2
B1
B3
B4
C1
C1
C2
C3
B5
B6
Case 8:
ART
HISTORY
CHINESE HISTORY
THREE KINDOM
RESEARCHES ON CAOCAO
RESEARCHES ON LIUBEI
CHINESE MORDEN HISTORY
JAPANESE HISTORY
JAPANESE ACIENT HISTORY
MATH
GRAPH THEORY
Case 9:
a b
b c
d e
d e
d e
Case 10:
ART
HISTORY
CHINESE HISTORY
THREE KINDOM
RESEARCHES ON CAOCAO
RESEARCHES ON LIUBEI
CHINESE MORDEN HISTORY
JAPANESE HISTORY
JAPANESE ACIENT HISTORY
MATH
GRAPH THEORY
Case 11:
0
0
0
0
1
Case 12:
1
2
3
2
4
Case 13:
a
0
Case 14:
0 0 0
0000
0000
0
0000
0000
0000
0000
Case 15:
0
a
a
b
c
a
b
c
d
e
f
g
a
b
Case 16:
A
B
C
B
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;
}