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_实现给未来元素添加事件。
未来元素:不是一个页面上的元素,是通过js或者通过后台直接渲染在页面上的元素,也就是说这些元素不是直接写在document中的. 1.对于未来元素,我们想直接用js或者jq操作它们是不起作用的. $( ...
- 解决嵌套GridView显示不全的问题
package com.adan.selectcitydome.view; import android.content.Context; import android.util.AttributeS ...
- java===java基础学习(12)---方法的重写和重载
覆盖 / 重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也 ...
- 64_l3
libguac-client-ssh-0.9.13-3.20170521git6d2cfda...> 23-May-2017 09:58 64570 libguac-client-ssh-0.9 ...
- Linux(Unix)密码策略问题导致root密码不能修改
Linux(Unix)密码策略问题导致root密码不能修改 发布时间: 2016-01-19 浏览次数: 1034 下载次数: 5 用户修改了密码配置文件,导致root账户修改密码时报如下错误: ...
- Mongo 配置文件 [www]
Mongo 配置文件 [www] http://blog.chinaunix.net/uid-25206403-id-3510934.html mongodb 安装使用 http://blog.si ...
- nodejs 优雅的连接 mysql
1.mysql 及 promise-mysql nodejs 连接 mysql 有成熟的npm包 mysql ,如果需要promise,建议使用 promise-mysql: npm:https:// ...
- 81.Search in Rotated Sorted Array II---二分变形
题目链接 题目大意:与33题类似,只是这里数组中有重复数值. 法一:解法与33题类似,只是这里要处理1,3,1,1,1这种情况,即有重复值时,mid与left和right都相等时,可以采用right- ...
- C基础 常用设计模式粗解
引言 面向对象, 设计模式是现代软件开发基石. C的面向过程已经很简洁, 但不代表C就没有面向对象.(libuv框架中C面向对象用的很多) 因为思想是互通的.全当熟悉一下那些常用的设计模式.先假定有一 ...
- 2.C 基础
C 基础 原文地址:http://rypress.com/tutorials/objective-c/c-basics OC 可以说是C语言的一个超集,这样你可以无缝的和C语言结合编程也就是你可以这两 ...