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

注意,即使没有书归还,当遇到SHELVE后依然要输出一个END

Posted by uuuouou at 2014-09-23 20:38:44 on Problem 1886
#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:
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