The Book List
-
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
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:
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.
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.
Output
For each test case, print "Case n:" first(n starts from 1), then print the new list as required.
注意:文件夹中的文件要按照windows的排列方式,即所有的文件夹先显示在前面,再显示文件,文件夹与文件各自都要按照字典序排序
相同的文件只要输出一次
思路:明显的字典树,可是不会处理各种字符串,后面看了别人的思路才懂。
注意map和set一样是会自动把里面的元素按照字典序排序的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
struct Node
{
map<string,int> ma;
map<string,int> ma1;
} p[3050];
void dfs(int u,int deep)
{
for(map<string, int>::iterator iter = p[u].ma.begin(); iter != p[u].ma.end();++iter)
{
for(int i=1;i<=4*deep;i++)
printf(" ");
cout<<iter->first<<endl;
dfs(iter->second,deep+1);
}
for(map<string,int> ::iterator iter=p[u].ma1.begin();iter!=p[u].ma1.end();++iter)
{
for(int i=1;i<=4*deep;i++)
printf(" ");
cout<<iter->first<<endl;
}
}
int main()
{
char a[3050];
int num=0,Tcase=1;
for(int i=0;i<3050;i++) p[i].ma.clear(),p[i].ma1.clear();
while(gets(a))
{
int len=strlen(a);
if(len==1&&a[0]=='0')
{
printf("Case %d:\n",Tcase++);
dfs(0,0);
for(int i=0;i<3050;i++) p[i].ma.clear(),p[i].ma1.clear();
num=0;
continue;
}
int cnt=0;
a[len]='@';
string m="";
for(int i=0; i<=len; i++)
{
if(a[i]=='/')
{
if(p[cnt].ma.find(m)==p[cnt].ma.end())
{
p[cnt].ma[m]=++num;
cnt=num;
m="";
}
else
{
cnt=p[cnt].ma[m];
m="";
}
}
else if(a[i]=='@')
{
if(p[cnt].ma1.find(m)!=p[cnt].ma1.end()) continue;
p[cnt].ma1[m]=++num;
}
else m+=a[i];
}
}
return 0;
}
北京大学图书馆拥有丰富的藏书资源,本文介绍了一个图书分类系统的重构任务,旨在通过调整图书及其分类的展示方式来提高检索效率。系统采用字典树结构进行排序,并确保同一类别下的图书和子类别按字典序排列。
672

被折叠的 条评论
为什么被折叠?



