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 ...
随机推荐
- windows下安装python过程
方法一:如果你的电脑没有安装python,推荐使用anaconda(自带python环境,同时自带各种第三方库,可以省去很多麻烦) 这里提供两个下载地址:1,.官网https://www.anacon ...
- 破解邻居家的wifi密码
刚刚学习了如何破解wifi密码 然后昨天晚上连续破解了两个 好激动 我是在ubuntu上面使用aircrack-ng套件进行破解的 首先进行抓包,然后跑字典就ok了 下面的不错 一.关闭网络和结束可能 ...
- 從 kernel source code 查出 版本號碼
kernel/Makefile 1 VERSION = 4 2 PATCHLEVEL = 4 3 SUBLEVEL = 21 4 EXTRAVERSION = 5 NAME = Blurry Fish ...
- 大公司开源网址[www]
https://github.com/blackberry https://github.com/CallForSanity?tab=repositories https://github.com/b ...
- 【SCOI2010】维护序列
NOI2017的简化版…… 就是维护的时候要想清楚怎么讨论. #include<bits/stdc++.h> #define lson (o<<1) #define rson ...
- 【jzoj6.24模拟B】
这场真是无聊,搬远古原题…… xjb做了做,(居然没AK真是身败名裂) A.教主的花园 答案明显具有可二分性,二分答案判定下就行. #include<bits/stdc++.h> #def ...
- plus.networkinfo.getCurrentType()
HTML5+API device Device Device模块管理设备信息,用于获取手机设备的相关信息,如IMEI.IMSI.型号.厂商等.通过plus.device获取设备信息管理对象. 对象: ...
- [ Python ] set集合及函数的使用
1. set类型 set 和 dict 类似,也是一组 key 的集合,但是不存储 value. 由于 key 不重复,所以,在 set 中, 没有重复的 key 集合是可变类型 (1)集合的创建 ...
- StringBuilder类的作用,以及与String类的相互转换
# 转载请留言联系 先看一段String类的字符串拼接的代码. String s = "hello" 会在常量池开辟一个内存空间来存储”hello". s += &quo ...
- intellij idea 无法创建类文件,方法。