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

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread(ch) freopen(ch,"r",stdin) #define fwrite(ch) freopen(ch,"w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const double eps = 1e-8; const int maxn = 11234; struct Node {     map<string,int>nex;     set<string> hav; } arr[maxn]; int _cnt; int newNode() {     arr[_cnt].nex.clear();     arr[_cnt].hav.clear();     return _cnt++; } string spi[maxn]; void insert(int st,int dep,int len) {     if(dep == len - 1){         arr[st].hav.insert(spi[dep]);         return;     }     if(arr[st].nex.find(spi[dep]) == arr[st].nex.end())         arr[st].nex[spi[dep]] = newNode();     insert(arr[st].nex[spi[dep]],dep+1,len); } void insert(int root,string v) {     int len = 0;     spi[len] = "";     for(auto x : v)     {         if(x != '/')         {             spi[len] = spi[len] + string(1,x);         }         else         {             len++;             spi[len] = "";         }     }     len++;     insert(root,0,len); } void out(int st,int deep,string v) {     if(deep != -1)     {         for(int i=0; i<4*(deep); i++)             cout<<" ";         cout<<v<<endl;     }     for(auto x : arr[st].nex)     {         out(x.second,deep+1,x.first);     }     for(auto x : arr[st].hav){          for(int i=0; i<4*(deep + 1); i++)             cout<<" ";         cout<<x<<endl;     } } int main() {     //fread("in.in");     //fwrite("in.in");     string inp = "";     int icase = 1;     do     {         _cnt = 0;         int root = newNode();         if(inp[0] != '0')         {             if(inp != "")                 insert(root,inp);             while(getline(cin,inp) && inp != "0")             {                 insert(root,inp);             }         }         cout<<"Case "<<icase++<<":"<<endl;         out(root,-1,"");     }     while(getline(cin,inp));     return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化以下程序至150行,#include <stdio.h> #include <string.h> // 定义书籍结构体 typedef struct { char name[50]; char author[50]; char publisher[50]; char date[20]; float price; int location; int inventory; } Book; // 初始化书籍列表 const int BOOK_LIST_SIZE = 3; Book bookList[] = { {"The Great Gatsby", "F. Scott Fitzgerald", "Scribner", "1925", 9.99, 1, 10}, {"To Kill a Mockingbird", "Harper Lee", "J. B. Lippincott & Co.", "1960", 7.99, 2, 5}, {"1984", "George Orwell", "Secker and Warburg", "1949", 12.99, 3, 3} }; // 查询书籍信息并计算总价 void search_book(Book *book, int quantity) { printf("Book name: %s\n", book->name); printf("Author: %s\n", book->author); printf("Publisher: %s\n", book->publisher); printf("Date: %s\n", book->date); printf("Price: %.2f\n", book->price); printf("Location: %d\n", book->location); if (book->inventory >= quantity) { printf("Inventory: %d\n", book->inventory); float total_price = quantity * book->price; printf("Total price: %.2f\n", total_price); book->inventory -= quantity; } else { printf("Sorry, the required quantity is not in stock.\n"); printf("Current inventory: %d\n", book->inventory); } } // 查询书籍信息并购买 void purchase_book() { char name[50], author[50]; int quantity; printf("Please enter the book name: "); scanf("%s", name); printf("Please enter the author's name: "); scanf("%s", author); printf("Please enter the required quantity: "); scanf("%d", &quantity); for (int i = 0; i < BOOK_LIST_SIZE; i++) { if (strcmp(name, bookList[i].name) == 0 && strcmp(author, bookList[i].author) == 0) { search_book(&bookList[i], quantity); return; } } printf("Sorry, we don't have this book in stock.\n"); } // 展示所有书籍信息 void show_all_books() { printf("\n=============Our Book List=============\n"); for (int i = 0; i < BOOK_LIST_SIZE; i++) { printf("Book name: %s\n", bookList[i].name); printf("Author: %s\n", bookList[i].author); printf("Publisher: %s\n", bookList[i].publisher); printf("Date: %s\n", bookList[i].date); printf("Price: %.2f\n", bookList[i].price); printf("Location: %d\n", bookList[i].location); printf("Inventory: %d\n", bookList[i].inventory); printf("\n"); } } int main() { char flag; do { purchase_book(); printf("Do you want to purchase other books? (y/n): "); scanf(" %c", &flag); } while (flag == 'y' || flag == 'Y'); show_all_books(); return 0; }
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值