2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)
【题目传送门】
1383 : 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.
输入
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树牌大模拟)的更多相关文章
- 【icpc网络赛大连赛区】Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...
- Trace 2018徐州icpc网络赛 (二分)(树状数组)
Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- 2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组
2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组 [Problem Description] 给你一个\([1,n]\)的排列,查询\([l,r]\)区间内有多少对 ...
- 【2018ACM/ICPC网络赛】沈阳赛区
这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...
- 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 ...
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- 南京网络赛I-Skr【回文树模板】
19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...
- ACM-ICPC2018沈阳网络赛 Lattice's basics in digital electronics(模拟)
Lattice's basics in digital electronics 44.08% 1000ms 131072K LATTICE is learning Digital Electron ...
随机推荐
- js获取屏幕高度宽度
获取各种屏幕的宽度和高度Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽 ...
- import学习
一.import as import socket, os, regex模块导入时可以使用 as 关键字来改变模块的引用对象名字: import os as system //当多个引入时 ...
- Python自动化运维 - Django(二)Ajax基础 - 自定义分页
Ajax基础 AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下. 什么是Ajax AJAX = 异步 Java ...
- Ubuntu 14.04 安装gstreamer0.10-ffmpeg
sudo apt-add-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install -y gstreame ...
- Linux 入门记录:十五、Linux 网络基本配置
一.以太网(Ethernet) 以太网(Ethernet)是一种计算机局域网技术.IEEE 组织的 IEEE 802.3 标准制定了以太网的技术标准,它规定了包括物理层的连线.电子信号和介质访问层协议 ...
- 【HDU5306】Gorgeous Sequence
这个题目是Segment-Tree-beats的论文的第一题. 首先我们考虑下这个问题的不同之处在于,有一个区间对x取max的操作. 那么如何维护这个操作呢? 就是对于线段树的区间,维护一个最大值标记 ...
- 【uva11248】网络扩容
网络流裸题. 求完最大流之后保留残余容量信息,依次将已经加入最小割的弧变成c再跑,记录下即可. #include<bits/stdc++.h> #define N 20005 #defin ...
- php之trait-实现多继承
PHP是单继承的语言,在PHP 5.4 Traits出现之前,PHP的类无法同时从两个基类继承属性或方法.php的Traits和Go语言的组合功能类似,通过在类中使用use关键字声明要组合的Trait ...
- LeetCode解题报告—— Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- hdu 1864(01背包,输入处理真烦)
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...