http://hihocoder.com/problemset/problem/1383?sid=950389

#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.

这题主要是考察坑爹点。

首先是用字典树解决的了。

我的做法比较复杂,因为一开始的时候没想到用map来离散那些字符串。

所以我的就是,在字典树中开多了个数组来保存字符串,

struct node {
  char has[30 + 2][100 + 2]; //用来保存同一级目录下是否的字符串
  int use;            //判断有多少个字符串了
  struct node * pNext[30 + 2]; //然后第i个字符串的下一个是谁,相当于离散化了。
} tree[10000 + 2];

坑爹

1:用"0"结束,那么你判断name[1] == '0'就GG了,因为有可能是0ABC这样的数据。

2:目录和文件名是不同的,A/B  和 A  其中的A是不同的,因为是不同含义,第二个是文件名

3:相同的只算一次。

4:四个空格和一个\t不等价

还有就是一个很坑爹的地方,

就是样例那里了,为什么不是按字典序输出,而且有些比较长的目录优先输出。

这样的话,又要判断了,优先输出有孩子的那个。

我的代码看不懂的了,可以用来找样例吧,我给几组数据。

A
A
A
A/A
A/A/A
A/A/A
A/A/A/A/A
A/A/A/A/A/A
A
A/A/A
A/A/A/A/A
AA/A
0

A/B/C

A/B

0

A/B

A

0A

A

我这里要倒这排序这些字符串,因为要解决

A/B

A

先插入A/B

还有判断优先输出有儿子的,有就是有,不管有多少个。取值为1 or 0

dfs输出的时候,可以用cur表示再它前面已经输出了多少个,那么输出对应空格即可。

感觉我这个字典树用的还是比较6的,不过细节那么多,做了很久。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> struct bookList {
char name[ + ];
bool operator < (const struct bookList & rhs) const {
return strcmp(name + , rhs.name + ) > ;
}
} book[ + ];
struct node {
char has[ + ][ + ];
int use;
struct node * pNext[ + ];
} tree[ + ];
struct TT {
int id;
int nextChild;
bool operator < (const struct TT & rhs) const {
if (nextChild != rhs.nextChild) return nextChild > rhs.nextChild;
else return id > rhs.id;
}
}FOR[ + ][ + ];
int all;
int t;
struct node * create() {
struct node * p = &tree[t++];
p->use = ;
for (int i = ; i <= ; ++i) {
memset(p->has[i], , sizeof p->has[i]);
p->pNext[i] = NULL;
}
return p;
}
void insert(struct node **T, char str[], int lenstr) {
struct node * p = *T;
if (p == NULL) {
*T = p = create();
}
str[lenstr + ] = '/';
char sub[];
int lensub = ;
for (int i = ; i <= lenstr + ; ++i) {
if (str[i] == '/') {
sub[lensub + ] = '\0';
bool flag = false;
for (int j = ; j <= p->use && i != lenstr + ; ++j) {
if (strcmp(sub + , p->has[j] + ) == ) {
p = p->pNext[j];
flag = true;
break;
}
}
if (flag == false) {
p->use++;
strcpy(p->has[p->use] + , sub + );
p->pNext[p->use] = create();
p = p->pNext[p->use];
}
lensub = ;
} else sub[++lensub] = str[i];
}
}
int liu;
char str1[], str2[];
void dfs(struct node *T, int cur, struct TT tosort[]) {
if (T == NULL) return;
struct node *p = T;
for (int i = ; i <= p->use; ++i) {
tosort[i].id = i;
tosort[i].nextChild = p->pNext[i]->use > ;
}
sort(tosort + , tosort + + p->use);
for (int i = ; i <= p->use; ++i) {
for (int j = ; j <= cur; ++j) {
for (int k = ; k <= ; ++k) printf(" ");
}
printf("%s\n", p->has[tosort[i].id] + );
dfs(p->pNext[tosort[i].id], cur + , FOR[++liu]);
}
}
int f = ;
void work() {
while (gets(book[++all].name + ) != NULL && strcmp(book[all].name + , "") != ) ;
all--;
sort(book + , book + + all);
// for (int i = 1; i <= all; ++i) {
// printf("%s\n", book[i].name + 1);
// }
// printf("\n");
struct node *T = NULL;
memset(str1, , sizeof str1);
memset(str2, , sizeof str2);
strcpy(str2 + , book[].name + );
for (int i = ; i <= all; ++i) {
// printf("%s %s\n", str1 + 1, str2 + 1);
if (strcmp(str1 + , str2 + ) == ) {
strcpy(str1 + , str2 + );
strcpy(str2 + , book[i + ].name + );
continue;
}
strcpy(str1 + , str2 + );
strcpy(str2 + , book[i + ].name + );
insert(&T, book[i].name, strlen(book[i].name + ));
}
printf("Case %d:\n", ++f);
liu = -;
dfs(T, , FOR[++liu]);
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (gets(book[++all].name +) != NULL && strcmp(book[all].name + , "") != ) {
work();
// printf("\n");
all = ;
t = ;
// memset(tree, 0, sizeof tree);
// memset(FOR, 0, sizeof FOR);
// memset(book, 0, sizeof book);
}
return ;
}

hihoCoder 1383 : The Book List 北京网络赛的更多相关文章

  1. hihocoder1236(北京网络赛J):scores 分块+bitset

    北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...

  2. 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

    2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...

  3. 2015北京网络赛 J Scores bitset+分块

    2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...

  4. 2015北京网络赛 Couple Trees 倍增算法

    2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 q ...

  5. HDU-4041-Eliminate Witches! (11年北京网络赛!!)

    Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. hihoCoder 1391 Countries【预处理+排序+优先队列】2016北京网络赛

    题目:http://hihocoder.com/problemset/problem/1391 题目大意: A和B两个国家互射导弹,每个国家都有一个防御系统,在防御系统开启的时间内可以将到达本国的导弹 ...

  7. 2016北京网络赛 hihocoder 1391 Countries 树状数组

    Countries   描述 There are two antagonistic countries, country A and country B. They are in a war, and ...

  8. hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

    时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...

  9. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

随机推荐

  1. java自定义类型 作为HashMap中的Key值 (Pair<V,K>为例)

    由于是自定义类型,所以HashMap中的equals()方法和hashCode()方法都需要自定义覆盖. 不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals方法, ...

  2. BZOJ1861:[ZJOI2006]书架

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  3. Visual Studio 编译后去掉只读属性

    Visual Studio 编译后去掉只读属性 attrib $(TargetPath) -R attrib $(TargetDir)$(TargetName).pdb -R

  4. HDU-5974

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  5. url&nbsp;传递参数(特殊字符)解决方法

    url 传递参数(特殊字符)解决方法 首先设置 apache 配置文件, server.xml 在 port=8080 那一行中加上 URIEcoding=GBK 有些符号在URL中是不能直接传递的, ...

  6. sql中内联&nbsp;和外联&nbsp;区别

    sql中内联 和外联 区别 2007-05-15 17:37 这个概念一般看书不好理解.其实夜简单.有例子就简单了. 比如: 表A(主表) cardid username 16 aa 23 bb 25 ...

  7. pysam操作sam文件

    pysam模块 因为要分析sam文件中序列的情况,因此要对reads进行细分,所以之前想用数据库将sam文件信息存储,然后用sql语句进行分类.后来发现很麻烦,pysam就是一个高效读取存储在SAM ...

  8. adt eclipse 配置问题 error:could not open ...jvm.cfg

    在安装adt eclipse后,打开eclipse提示error:could not open eclipse\jre\lib\amd64\jvm.cfg 据网上显示原因应该是以前安装过java,然后 ...

  9. Spring入门第八课

    看如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  10. IIS 6.0曝远程代码执行漏洞CVE-2017-7269

    一.漏洞说明 漏洞编号:CVE2017-7269 影响中间件:IIS6.0 影响服务器版本:windows 2003 R2 二. 环境搭建 虚拟机kali : 192.168.1.2 靶机window ...