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
样例输出
-
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.
题意:给你若干本书,0输入结束,每层代表它的所属,输出的方法是,如果它当前目录是最后的,那就放到所属这一层最后输出,否则按字典序输出。
题解:递归分治,写的有点麻烦,代码会解释清楚。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[35][105];
struct node{
string s[35];
int num;
}tree[35];
int depth;
bool cmp(node a,node b){
if(a.num==depth&&b.num==depth){//如果两本书都是最后目录 字典序排序
return a.s[depth].compare(b.s[depth])<0;
}
else if(a.num==depth)return 0;//如果a是最后的
else if(b.num==depth)return 1;//如果b是最后的
else{
return a.s[depth].compare(b.s[depth])<0;//字典序
}
}
void solve(int l,int r,int dep){
depth=dep;//当前目录
sort(tree+l,tree+1+r,cmp);//处理l r区间的排序
int ss=l,i;
int rig=r+1;//代表是最后目录的书的位置
for(i=l;i<=r;i++){
if(tree[i].num==dep){//寻找rig
rig=i;
break;
}
}
for(i=l+1;i<rig;i++){//然后对此区间分治处理 找出本层目录相同字母的区间
if(tree[i].s[dep].compare(tree[ss].s[dep])){//如果找到了区间
for(int sa=1;sa<=(dep-1)*4;sa++)printf(" ");//输出空格
cout<<tree[ss].s[dep]<<endl;//输出本层目录
solve(ss,i-1,dep+1);//然后对这一层进行分治
ss=i;
}
}
if(ss<rig){
for(int sa=1;sa<=(dep-1)*4;sa++)printf(" ");//最后一个区间
cout<<tree[ss].s[dep]<<endl;
solve(ss,rig-1,dep+1);
}
for(i=rig;i<=r;i++){
for(int sa=1;sa<=(dep-1)*4;sa++)printf(" ");//输出最后目录的书
cout<<tree[i].s[dep]<<endl;
}
}
int main(){
int cas=1;
while(gets(a[1]+1)){
if(a[1][1]=='0'&&a[1][2]=='\0'){//如果是0
printf("Case %d:\n",cas++);
break;
}
int n=1;
while(gets(a[++n]+1)){//有空格 用gets
if(a[n][1]=='0'&&a[n][2]=='\0'){
n--;
break;
}
int flag=1;
for(int i=1;i<n;i++){
if(strcmp(a[i]+1,a[n]+1)==0){//如果重复了
n--;
flag=0;
break;
}
}
if(!flag)continue;
}
char d[105];
int i,j;
for(i=1;i<=35;i++){//这本书最多有几个目录
tree[i].num=0;
}
for(i=1;i<=n;i++){
int len=0;
for(j=1;j<=strlen(a[i]+1);j++){//分割目录
if(a[i][j]=='/'){
d[len]='\0';
tree[i].s[++tree[i].num]=d;
len=0;
}
else d[len++]=a[i][j];
}
d[len]='\0';
tree[i].s[++tree[i].num]=d;
}
printf("Case %d:\n",cas++);
solve(1,n,1);
}
return 0;
}