题目传送门

1383 : 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 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.

样例输入

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

字典树大模拟。因为输出要按字典序输出,所以输入的时候需要给字符串排个序。而且输出的时候在相同层有后继的优先输出(题目没说清QAQ),所以在输出上要一点小处理。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define maxnode 3010
using namespace std;
struct node
{
int lt,rt;
char *str;
int val;
};
struct trie
{
node poi[maxnode];
int nodelen;
int head;
trie() {nodelen=; head=; clr(poi);}
void clear() {nodelen=;head=; clr(poi);}
void insert(int fa,int now,char *stri)
{
// printf("fat:%d p:%d nodelen:%d head:%d char:%s\n",fa,now,nodelen,head,stri);
int end=false,i=;
while(stri[i] && stri[i]!='/')
i++;
if(stri[i]==)
end=true;
stri[i]=;
if(!now)
{
now=newnode(stri);
poi[fa].lt=now;
if(!end)
insert(now,,stri+i+);
else
poi[now].val++;
return ;
}
int q;
while(now && strcmp(poi[now].str,stri)!=)
{
q=now;
now=poi[now].rt;
}
if(!now)
{
now=newnode(stri);
poi[q].rt=now;
}
if(!end)
{
insert(now,poi[now].lt,stri+i+);
}
else
{
poi[now].val++;
}
return ;
}
int newnode(char *stri)
{
if(!head)
{
head=nodelen;
}
poi[nodelen].str=stri;
return nodelen++;
}
void output(int node,int dep)
{
if(node==)
{
return ;
}
int q=node;
while(q)
{
if(poi[q].lt!=)
{
for(int i=;i<dep;i++)
printf(" ");
printf("%s",poi[q].str);
printf("\n");
output(poi[q].lt,dep+);
}
q=poi[q].rt;
}
q=node;
while(q)
{
if(poi[q].val)
{
for(int i=;i<dep;i++)
printf(" ");
printf("%s",poi[q].str);
printf("\n");
}
q=poi[q].rt;
}
return ;
} }tried;
bool cmp(char *a,char *b)
{
return strcmp(a,b)<;
}
char s[],deal[maxnode][];
char *dir[maxnode];
int main()
{
int n,kase=;
while(fgets(s,,stdin)!=NULL)
{
clr(deal);
n=;
s[strlen(s)-]='\0';
tried.clear();
strcpy(deal[n++],s);
dir[]=deal[];
while(fgets(s,,stdin)!=NULL && strcmp(s,"0\n")!=)
{
s[strlen(s)-]='\0';
strcpy(deal[n],s);
dir[n]=deal[n];
n++;
}
sort(dir+,dir+n,cmp);
for(int i=;i<n;i++)
{
tried.insert(,tried.head,dir[i]);
}
printf("Case %d:\n",++kase);
tried.output(tried.head,);
}
return ;
}

2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)的更多相关文章

  1. 【icpc网络赛大连赛区】Sparse Graph

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...

  2. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

  3. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  4. 2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组

    2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组 [Problem Description] ​ 给你一个\([1,n]\)的排列,查询\([l,r]\)区间内有多少对 ...

  5. 【2018ACM/ICPC网络赛】沈阳赛区

    这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K  Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...

  6. Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

    Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...

  7. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  8. 南京网络赛I-Skr【回文树模板】

    19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...

  9. ACM-ICPC2018沈阳网络赛 Lattice's basics in digital electronics(模拟)

    Lattice's basics in digital electronics 44.08% 1000ms 131072K   LATTICE is learning Digital Electron ...

随机推荐

  1. 用 Docker 来构建 Jumpserver

    说明: 项目从 [ Jumpserver 官方 ] fork 而来. 主要更新: OS: ubuntu:18.04 优化了 Dockerfile Jumpserver 版本: 1.4.0 redis ...

  2. 使用node.js做一个自用的天气插件

    var request = require('request') var url = 'http://www.baidu.com/home/xman/data/superload' var cooki ...

  3. jstorm相关

    https://www.cnblogs.com/antispam/p/4182210.html

  4. C++ 流操作符重载函数

    1. 问题 在C++中,在进行输入输出操作时,我们首先会想到用cout, cin这两个库操作语句来实现,比如 cout << 8 << "hello world!&q ...

  5. English——Unit 1

    meditate  v.沉思,冥想:考虑,谋划 medtiation   n.沉思,冥想:深思熟虑 medium elaborate   adj.精心制作的,详尽的,复杂的:v.精心制作:详述(计划, ...

  6. hihocoder 1145 : 幻想乡的日常

    #1145 : 幻想乡的日常 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 幻想乡一共有n处居所,编号从1到n.这些居所被n-1条边连起来,形成了一个树形的结构. 每处 ...

  7. Fraction to Recurring Decimal——数值处理&&哈希表

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  8. centos 7 防火墙设置

    一.介绍 centos 7 的防火墙是以firewalld daemon的形式存在,区别于iptables 二.使用方法 centos7 主要通过firewall-cmd命令来管理firewall, ...

  9. 关于在C#对类的属性理解

    在类中都有一些成员.什么是类中的成员呢,我个人理解的是一个类中所应有的属性,方法,字段(因为目前才接触到类.所以类中一些其它应有的东西还不太熟悉),现在只探讨我列举的这几个在类中应有的东西.什么是属性 ...

  10. DOM方法index()相关问题(为何$(this).index(this)是错误的 )

    写jQuery的时候遇到一个关于index()的问题,查找相关资料后,解决了,把自己的想法写在下面. index() 方法返回指定元素相对于其他指定元素的 index 位置. 完全语法为:$(sele ...