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 |
可以用STL的set. 有个upper_bound很好用。只要把书从大到小排序RT.. #include<iostream> #include<set> #include<map> #include<string> using namespace std; struct Book{ string title; string author; }; bool operator<(const Book &b1,const Book &b2){ if(b1.author==b2.author) return b1.title>b2.title; return b1.author>b2.author; } set<Book> library; set<Book> desk; map<string,Book> title_to_book; int main() { string line; //build library while(getline(cin,line),line!="END"){ int p = line.find(" by "); Book book; string title = line.substr(0,p); string author = line.substr(p+4); book.title = title; book.author = author; title_to_book[title] = book; library.insert(book); } while(getline(cin,line),line!="END"){ string title; if(line.substr(0,6)=="BORROW"){ title = line.substr(7); library.erase(title_to_book[title]); }else if(line.substr(0,6)=="RETURN"){ title = line.substr(7); desk.insert(title_to_book[title]); }else{ //shelve for(set<Book>::reverse_iterator it=desk.rbegin();it!=desk.rend();it++){ cout<<"Put "<<it->title; if(library.upper_bound(*it)==library.end()) cout<<" first"<<endl; else cout<<" after "<<library.upper_bound(*it)->title<<endl; library.insert(*it); } cout<<"END"<<endl; desk.clear(); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator