惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
博客园 - 叶小钗
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
腾讯CDC
博客园 - Franky
博客园 - 聂微东
V
Visual Studio Blog
GbyAI
GbyAI
Martin Fowler
Martin Fowler
罗磊的独立博客
Y
Y Combinator Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
hihocoder 1383.The Book List(ACM-ICPC国际大学生程序设计竞...
2016-09-24 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

描述

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

题解

纯模拟即可,可以用树存储,也可以用链表存储,任意一种方法都行

使用 链表 纯模拟即可,需要注意一些细节的处理
优先输出书库,再输出书本,同时应该按照字典序输出

因此,可以用两个不同的链表分别维护子书库和书本

直接使用 STL 即可,单纯考验 STL 的熟练度

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

typedef long long LL;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

int kase = 1;

struct CATEGORY {
    list<CATEGORY> *child;
    list<CATEGORY> *book;
    string s;
    CATEGORY(string b) {
        list<CATEGORY> *p = new list<CATEGORY>;
        p->clear();
        child = p;

        p = new list<CATEGORY>;
        p->clear();
        book = p;

        s = b;
    }
    bool operator < (string rhs) {
        return s < rhs;
    }
};

list<CATEGORY> L;

list<CATEGORY>::iterator pushsk(list<CATEGORY>::iterator t,string s) {
    list<CATEGORY>::iterator it = t->child->begin();
    it = lower_bound(t->child->begin(),t->child->end(),s);
    if(it == t->child->end() || it->s != s) {
        t->child->insert(it,CATEGORY(s));
        it--;
    }
    return it;
}

void pushs(list<CATEGORY>::iterator t,string s) {
    list<CATEGORY>::iterator it = t->book->begin();
    it = lower_bound(t->book->begin(),t->book->end(),s);
    if(it == t->book->end() || it->s != s) {
        t->book->insert(it,CATEGORY(s));
        it--;
    }
}

void DFS(list<CATEGORY> *L,int n) {
    if(L == NULL)
        return;
    list<CATEGORY>::iterator it = L->begin();
    while(it != L->end()) {
        for(int i = 0;i < n;i++)
            cout << "    ";
        if(n != -1)
            cout << it->s << endl;
        DFS(it->child,n + 1);
        DFS(it->book,n + 1);

        delete it->child;
        delete it->book;//处理内存泄露问题
        ++it;
    }
}

bool Do() {
    L.clear();
    L.push_back(CATEGORY("MAIN"));//主书库

    string s;
    while(1) {
        if(!(getline(cin,s)))
            return false;
        if(s == "0")
            break;
        size_t len = s.size();
        string t = "";
        list<CATEGORY>::iterator it = L.begin();

        for(size_t i = 0;i < len;i++) {
            if(s[i] == '/') {
                it = pushsk(it,t);
                t = "";
            } else {
                t += s[i];
            }
        }
        pushs(it,t);
    }
    cout << "Case " << kase++ << ":" << endl;
    DFS(&L,-1);
    return true;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    while(Do());

    return 0;
}