Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

可以用STL的set. 有个upper_bound很好用。只要把书从大到小排序

Posted by Liuzhaoliang at 2014-10-06 04:26:59 on Problem 1886
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator