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 " by author
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 " title1 " after " title2 "
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"
SHELVEEND
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
题意
模拟图书馆借书还书(按作者字典序从小到大,再按书名从小到大),BRROW借一本书,RETURN还一本书,SHELVES把书按序一本本放回书架
题解
由于借书还书只给你书名,所以需要用map<string,string>把书名映射到作者,这样才可以用set查找
BRROW借书:就是删除erase一本书的作者和书名
RETURN还书:就是把作者和书名放进set1中
SHELVES放回书架:就是把遍历所有已经还的书it,用lower-bound找到插入后的位子hit,如果当前set为空或者hit==set.begin()就输出first,否则输出hit前面1个(hit--),最后插入*it即可,别忘了最后输出END
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct book 4 { 5 string title,author; 6 book(string t,string a):title(t),author(a){} 7 bool operator <(const book &rhs)const 8 { 9 return author<rhs.author||(author==rhs.author&&title<rhs.title); 10 } 11 }; 12 int main() 13 { 14 //freopen("in.txt","r",stdin); 15 //freopen("out.txt","w",stdout); 16 string s; 17 map<string,string> ma; 18 set<book> se,se1; 19 while(getline(cin,s)) 20 { 21 if(s=="END")break; 22 int pos=s.find(" by "); 23 ma[s.substr(0,pos)]=s.substr(pos+4); 24 se.insert(book(s.substr(0,pos),s.substr(pos+4))); 25 } 26 while(getline(cin,s)) 27 { 28 if(s[0]=='E')break; 29 else if(s[0]=='S') 30 { 31 set<book>::iterator it,bit; 32 for(it=se1.begin();it!=se1.end();it++) 33 { 34 bit=se.lower_bound(*it); 35 cout<<"Put "<<it->title; 36 if(se.empty()||bit==se.begin()) 37 cout<<" first"<<endl; 38 else 39 cout<<" after "<<(--bit)->title<<endl; 40 se.insert(*it); 41 } 42 se1.clear(); 43 cout<<"END"<<endl; 44 } 45 else if(s[0]=='B') 46 se.erase(book(s.substr(7),ma[s.substr(7)])); 47 else if(s[0]=='R') 48 se1.insert(book(s.substr(7),ma[s.substr(7)])); 49 } 50 return 0; 51 }