Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:注意,即使没有书归还,当遇到SHELVE后依然要输出一个ENDIn Reply To:注意,即使没有书归还,当遇到SHELVE后依然要输出一个END Posted by:uuuouou at 2014-09-23 20:38:44 > #include <iostream> > #include <string> > #include <set> > #include <map> > using namespace std; > > struct Book{ > string title; > string author; > Book(){} > Book(const string& t, const string& a): title(t), author(a){} > }; > class Library{ > private: > map<string, string> dataBase; > struct Compare{ > bool operator()(const Book& a, const Book& b){ > if(a.author != b.author) return a.author < b.author; > return a.title < b.title; > } > }; > set<Book, Compare> stock; > set<Book, Compare> returned; > public: > void Add(const Book& book){ > stock.insert(book); > dataBase[book.title] = book.author; > } > void Borrow(const string& title){ > stock.erase(Book(title, dataBase[title])); > } > void Return(const string& title){ > returned.insert(Book(title, dataBase[title])); > } > void Shelve(){ > set<Book, Compare>::iterator iter = returned.begin(), eter = returned.end(), it; > for(; iter != eter; ++iter){ > it = stock.insert(*iter).first; > cout << "Put \"" << iter->title << "\""; > if(it == stock.begin()) cout << " first\n"; > else cout << " after \"" << (--it)->title << "\"\n"; > } > cout << "END\n"; > returned.clear(); > } > }; > > int main() > { > ios::sync_with_stdio(false); > > string s, END("END"); > string::size_type pos; > Library lib; > > while(getline(cin, s), s != END){ > pos = s.rfind('\"'); > lib.Add(Book(s.substr(1, pos - 1), s.substr(pos + 5))); > } > while(getline(cin, s), s != END){ > if(s[0] == 'B') lib.Borrow(s.substr(8, s.size()-9)); > else if(s[0] == 'R') lib.Return(s.substr(8, s.size()-9)); > else lib.Shelve(); > } > > return 0; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator