北京赛区(2016)网络赛 题目1 : The Book List 【字典树】
2016-09-24 18:51
337 查看
题目1 : The Book List
时间限制: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 volumeswere 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.样例输入
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
题解:
先插入字典树,然后对字典树上的元素进行排序;
排序遵守两个原则:
1. 类别名优先于书名。
2. 字典序小的优先于字典序大的。
注意:
结束是“一个” 0
单个换行可能有影响---
代码:
#include<cstdio> #include<cstring> #include<algorithm> struct trie{ int lei; char ch[101]; trie * next[33]; }boot,PP[3000],op[50]; int kp,lp; int ssscmp(trie * A,trie * B) { if (A->lei!=B->lei) return A->lei-B->lei; return strcmp(A->ch,B->ch); } void ADD(int ll) { trie * kk=&boot; for (int i=0;i<ll;i++) { bool fafe=true; int j; for (j=0;;j++) { if (kk->next[j]==NULL) break; if (ssscmp(kk->next[j],&op[i])==0) { fafe=false; kk=kk->next[j]; break; } } if (fafe) { strcpy(PP[kp].ch,op[i].ch); PP[kp].lei=op[i].lei; kk->next[j]=&PP[kp]; kk=kk->next[j]; kp++; } } } void pan(trie * xx) { trie * c; for (int i=0;xx->next[i]!=NULL;i++) { for (int j=i+1;xx->next[j]!=NULL;j++) { if (ssscmp(xx->next[i],xx->next[j])>0) { c=xx->next[i]; xx->next[i]=xx->next[j]; xx->next[j]=c; } } } for (int i=0;xx->next[i]!=NULL;i++) pan(xx->next[i]); } void output(trie * xx,int ko) { if (ko!=-1) { for (int i=0;i<ko;i++) { for (int j=0;j<4;j++) printf(" "); } printf("%s\n",xx->ch); } for (int i=0;xx->next[i]!=NULL;i++) { output(xx->next[i],ko+1); } } int main() { char ch[101]; kp=0; int ca=1; while (gets(ch)) { if (ch[0]==0) continue; if (ch[0]=='0'&&strlen(ch)==1) { pan(&boot); printf("Case %d:\n",ca++); output(&boot,-1); kp=0; memset(PP,0,sizeof(PP)); memset(op,0,sizeof(op)); for (int i=0;i<33;i++) boot.next[i]=NULL; } else { lp=0;int kl=0; int ll=strlen(ch); memset(op,0,sizeof(op)); for (int i=0;i<=ll;i++) { if (ch[i]=='/'||ch[i]==0) { if (ch[i]=='/') op[lp].lei=0; else op[lp].lei=1; lp++; kl=0; } else op[lp].ch[kl++]=ch[i]; } ADD(lp); } } }
相关文章推荐
- 2016北京网络赛 hihocoder 1391 Countries 树状数组
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
- HttpURLConnection的GET请求POST请求小总结
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- 学习python写网络爬虫(四)
- Linux C网络编程基本知识
- 网络实时流量监控工具iftop---转
- 理解 LSTM 网络
- TCP三次握手和四次挥手
- Android上使用HttpURLConnection发送HTTP请求
- HTTP中的重定向和请求转发的区别
- 从一个带有http://网络文件中获取文件名
- 关于w5500客户端和服务器的调试心得
- Kafka源码深度解析-序列4 -Producer -network层核心原理
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List
- 神经网络学习笔记 (四) BP神经网络
- httpd系列-01-常用配置
- 虚拟机【网络电缆被拔出】解决方法