Description

I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes.- (Charles Lamb, Essays of Elia (1823) `The Two Races of Men')
Like Mr. Lamb, librarians have their problems with borrowers too. People don't put books back where they should. Instead, returned books are kept at the main desk until a librarian is free to replace them in the right places on the shelves. Even for librarians, putting the right book in the right place can be very time-consuming. But since many libraries are now computerized, you can write a program to help.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically, the librarians will ask your program for a list of books that have been returned so the books can be returned to their correct places on the shelves. Before they are returned to the shelves, the returned books are sorted by author and then title using the ASCII collating sequence. Your program should output the list of returned books in the same order as they should appear on the shelves. For each book, your program should tell the librarian which book (including those previously shelved) is already on the shelf before which the returned book should go.
Input
First, the stock of the library will be listed, one book per line, in no particular order. Initially, they are all on the shelves. No two books have the same title. The format of each line will be:
``title" byauthor
The end of the stock listing will be marked by a line containing only the word:
END
Following the stock list will be a series of records of books borrowed and returned, and requests from librarians for assistance in restocking the shelves. Each record will appear on a single line, in one of the following formats:
BORROW ``title"
RETURN ``title"
SHELVE
The list will be terminated by a line containing only the word:
END
Output
Each time the SHELVE command appears, your program should output a series of instructions for the librarian, one per line, in the format:
Put `` " after ``
"
or, for the special case of the book being the first in the collection:
Put ``title" first
After the set of instructions for each SHELVE, output a line containing only the word:
END
Assumptions & Limitations:
1. A title is at most 80 characters long.
2. An author is at most 80 characters long.
3. A title will not contain the double quote (") character.
Sample Input
"The Canterbury Tales" by Chaucer, G. "Algorithms" by Sedgewick, R. "The C Programming Language" by Kernighan, B. and Ritchie, D. END BORROW "Algorithms" BORROW "The C Programming Language" RETURN "Algorithms" RETURN "The C Programming Language" SHELVE END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales" Put "Algorithms" after "The C Programming Language" END
最近状态不好,尤其在做第五章习题的时候 想的全是map set vector 其实这个在第五章习题里应该算是比较简单的 只要思路清晰就好过 这题被庄神点拨了一下 才发现真的是不用C++也能做的题 只要定义一个结构体 包括作者名 书名 是否在书架上的标志变量 刚开始做的时候还用了map vector set 。。呵呵呵对没错就是都用了 出现的几个问题就是 1.操作最后的end不是结束 而是继续读入新的 2.输出的顺序 刚开始还以为是按还回去的顺序输出 = = 所以一直错 反正然后我就重写了 就按结构体三种变量的那个 不贴代码了 然后我在vj上看到了另一种方法 思路差不多 代码很简洁 也有很多值得借鉴的地方 那贴这个好了。。#include<bits/stdc++.h> using namespace std; class book { public: book( string t, string a ) { author = a; title = t; borrowed = returned = false; } bool borrowed, returned; string author, title; }; vector<book> all; string in, command, req; void shelve(), borrow(), back(); bool cmpa( book a, book b ){ return ( a.author < b.author ); } bool cmpt( book a, book b ){ return ( a.title < b.title ); } int main() { while( getline( cin, in ) && in != "END" ) all.push_back( book( in.substr( 0, in.find_last_of( "\"" ) + 1 ), in.substr( in.find_last_of( "\"" ) + 1 ) ) ); stable_sort( all.begin(), all.end(), cmpt ); stable_sort( all.begin(), all.end(), cmpa ); while( cin >> command ) if( command == "BORROW" ) cin.get(), borrow(); else if( command == "RETURN" ) cin.get(), back(); else if( command == "SHELVE" ) cin.get(), shelve(); } void shelve() { for( int i = 0, j; i < all.size(); ++i ) if( all[ i ].returned == true ) { for( j = i; j >= 0; --j ) if( all[ j ].borrowed == false ) break; if( j == -1 ) printf( "Put %s first\n", all[ i ].title.c_str() ); else printf( "Put %s after %s\n", all[ i ].title.c_str(), all[ j ].title.c_str() ); all[ i ].borrowed = all[ i ].returned = false; } cout << "END\n"; } void borrow() { getline( cin, req ); for( int i = 0; i < all.size(); i++ ) if( all[ i ].title == req ) { all[ i ].borrowed = true; return; } } void back() { getline( cin, req ); for( size_t i = 0; i < all.size(); i++ ) if( all[ i ].title == req ) { all[ i ].returned = true; return; } }