HihoCoder - 1383 北京赛区(2016)网络赛——The Book List(字典树模拟)

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.

Sample 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

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 

题解:

第一反应感觉特别像字典树,只不过把建树元素从字母换成了字符串。

熟悉字典树的(不熟悉的可以通过这个找资料:ACM算法大全)立马会想到这会涉及到string的存储和查找,并且每个string还都会对应一个新的节点(struct实现)——这种有存储查找和映射关系的第一反应:“我大Map”。然后根据字典树改改就行了。

坑点:因为书和目录可能会有重名,而这里书和目录是分开的所以每个节点最好再开个set专门来存当前目录下有什么书。

另外因为map和set里自带排序所以可以不用管顺序了。(mapset大法好!!!♪(^∇^*))

代码:

#include <bits/stdc++.h>

using namespace std;

typedef struct Node* node;

struct Node{
	map<string,node> MMP;//存目录关系 
	set<string> BOOK;//存书 
};

void Insert(node root,string s){
	node p = root;
	string ss = "";
	for(int i=0 ; i<s.length() ; ++i){
		if(s[i] != '/')ss += s[i];
		else break;
	}
	if(s.length()-ss.length() == 0){
		p->BOOK.insert(ss);//这里不用判定书是否已经存在,因为set自带去重 
	}
	else {
		map<string,node>::iterator it;
		it = p->MMP.find(ss);
		if(it == p->MMP.end()){
			p->MMP[ss] = new Node();
		}
		Insert(p->MMP[ss],s.substr(ss.length()+1,s.length()-ss.length()));
	}
}

inline void Myput(char ch,int num){ 
	for(int i=1 ; i<=num ; ++i)printf("%c",ch);
}

void Print(node root,int N){
	map<string,node>::iterator it;
	for(it=root->MMP.begin() ; it!=root->MMP.end() ; ++it){
		Myput(' ',4*N);
		cout << it->first <<endl;;
		Print(it->second,N+1);
	}
	set<string>::iterator it2;
	for(it2=root->BOOK.begin() ; it2!=root->BOOK.end() ; ++it2){
		Myput(' ',4*N);
		cout << *it2 << endl;
	}
}

void Del(node root){
	map<string,node>::iterator it;
	for(it=root->MMP.begin() ; it!=root->MMP.end() ; ++it){
		Del(it->second);
	}
	delete(root);
}

int main(){
	
	string S;
	int Case = 0;
	while(getline(cin,S)){//因为名字中会有空格所以要直接按行读。 
		node root = new Node();
		while(true){
			if(S == "0")break;
			Insert(root,S);
			getline(cin,S);
		}
		printf("Case %d:\n",++Case);
		Print(root,0);
		Del(root);
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值