The Book List 2016北京网赛

图书分类系统重构
本文介绍了一个图书分类系统的实现方法,该系统使用多级目录结构来组织图书,并通过递归算法将原始分类列表转换为带有缩进的新列表,以便更直观地展示图书与分类之间的关系。
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.
输出

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

multimap直接模拟
注意当目录下 子目录名和书名相同的情况
还有就是 子目录排在书名之前

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

struct S{
    multimap<string,S>mp;
}tS;
multimap<string,S>mp;//当S中的multimap为空时 表示为一本书 否则是目录

void insert(multimap<string,S>&mp,string str){
    string tmp,f,b;//f为'/'前的部分 b为'/'后的部分
    for(string::iterator it=str.begin();it<str.end();++it){
        if(*it=='/'){
            tmp=str;
            f=tmp.assign(str.begin(),it);
            ++it;
            tmp=str;
            b=tmp.assign(it,str.end());
            multimap<string,S>::iterator it1,it2;
            it1=mp.lower_bound(f);//it1 it2保存string为f的范围
            it2=mp.upper_bound(f);
            bool flag=true;//flag=true表示需要创建名为f的子目录
            for(;it1!=it2;++it1){//尝试找有没有名为f的子目录
                if(!(it1->second.mp.empty())){
                    flag=false;
                    break;
                }
            }
            if(flag){
                mp.insert({f,tS});
                it1=mp.find(f);//此时所有key=f的 multimap都为空 随便find一个来添加都可以
            }

            if(!b.empty())
                insert(it1->second.mp,b);

            return;
        }
    }
    //没找到'/' 表示这是一本书
    multimap<string,S>::iterator it1,it2;
    it1=mp.lower_bound(str);//it1 it2保存string为f的范围
    it2=mp.upper_bound(str);
    bool flag=true;//=true表示没有找到同名的书
    for(;it1!=it2;++it1){
        if(it1->second.mp.empty()){//找到同名书了 不需要添加
            flag=false;
            break;
        }
    }
    if(flag)
        mp.insert({str,tS});
}

void print(int numOfSpace,multimap<string,S>&mp){
//先输出目录
    for(multimap<string,S>::iterator it=mp.begin();it!=mp.end();++it){
        if(!(it->second.mp.empty())){
            for(int i=0;i<numOfSpace;++i)
                printf("%4s","");
            cout<<it->first<<endl;
            print(numOfSpace+1,it->second.mp);//递归打印
        }
    }
    //再输出书
    for(multimap<string,S>::iterator it=mp.begin();it!=mp.end();++it){
        if(it->second.mp.empty()){
            for(int i=0;i<numOfSpace;++i)
                printf("%4s","");
            cout<<it->first<<endl;
        }
    }
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    string str;
    int c=1;
    while(getline(cin,str)){
        if(str=="0"){
            //work
            cout<<"Case "<<c++<<":"<<endl;
            print(0,mp);
            mp.clear();
        }
        else{
            //add
            insert(mp,str);
        }
    }
    return 0;
}
Delphi 12.3 作为一款面向 Windows 平台的集成开发环境,由 Embarcadero Technologies 负责其持续演进。该环境以 Object Pascal 语言为核心,并依托 Visual Component Library(VCL)框架,广泛应用于各类桌面软件、数据库系统及企业级解决方案的开发。在此生态中,Excel4Delphi 作为一个重要的社区开源项目,致力于搭建 Delphi 与 Microsoft Excel 之间的高效桥梁,使开发者能够在自研程序中直接调用 Excel 的文档处理、工作表管理、单元格操作及宏执行等功能。 该项目以库文件与组件包的形式提供,开发者将其集成至 Delphi 工程后,即可通过封装良好的接口实现对 Excel 的编程控制。具体功能涵盖创建与编辑工作簿、格式化单元格、批量导入导出数据,乃至执行内置公式与宏指令等高级操作。这一机制显著降低了在财务分析、报表自动生成、数据整理等场景中实现 Excel 功能集成的技术门槛,使开发者无需深入掌握 COM 编程或 Excel 底层 API 即可完成复杂任务。 使用 Excel4Delphi 需具备基础的 Delphi 编程知识,并对 Excel 对象模型有一定理解。实践中需注意不同 Excel 版本间的兼容性,并严格遵循项目文档进行环境配置与依赖部署。此外,操作过程中应遵循文件访问的最佳实践,例如确保目标文件未被独占锁定,并实施完整的异常处理机制,以防数据损毁或程序意外中断。 该项目的持续维护依赖于 Delphi 开发者社区的集体贡献,通过定期更新以适配新版开发环境与 Office 套件,并修复已发现的问题。对于需要深度融合 Excel 功能的 Delphi 应用而言,Excel4Delphi 提供了经过充分测试的可靠代码基础,使开发团队能更专注于业务逻辑与用户体验的优化,从而提升整体开发效率与软件质量。 资源来源于络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值