码迷,mamicode.com
首页 > 其他好文 > 详细

Borrowers

时间:2015-07-19 23:24:33      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

Description

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:

``titlebyauthor

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 `` 技术分享 " after `` 技术分享 "

or, for the special case of the book being the first in the collection:

Put ``titlefirst

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"
SHELVE
END

 

Sample Output

 

Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END


这道题完全是借鉴别人博客上的,不过当时没有理解深刻,所以犯了几个小错误
WA代码:
#include"iostream"
#include"cstring"
#include"sstream"
#include"set"
#include"map"
using namespace std;

struct node{
string name,author;
friend bool operator <(node a,node b){
if(a.author==b.author)
    return a.name<b.name;
else
    return a.author<b.author;
}
};

set<node> p;   //存放书架上原有书籍
set<node> q;   //存放借阅和归还信息
map<string,string> mp;

string fun(string c)
{
for(int i=0;i<c.size();i++)
{
    if(c[i]==\") c[i]=‘ ‘;
}
string cc,rev;
stringstream ss(c);
int f=0;
while(ss>>cc)
{
    if(f++) rev+=‘ ‘;
    rev+=cc;
}
return rev;
}

void deal(string temp)    //存好书架上的信息
{
    for(int i=0;i<temp.size();i++)
    {
        if(temp[i]==\") temp[i]=‘ ‘;
    }
    stringstream  ss(temp);
    string s;
    int flag=0,f=0;
    node e;
     while(ss>>s)
     {
         if(s=="by")
         {
            flag=1;
         }
         if(flag!=1)
         {
           if(f++) e.name+=‘ ‘;
           e.name+=s;
         }
         if(flag==1)
         {
             if(f++) e.author+=‘ ‘;
             e.author+=s;
         }
     }
     p.insert(e);
     mp[e.name]=e.author;
}


int main()
{
   string temp;
   while(getline(cin,temp))
   {
       if(temp=="END") break;
       deal(temp);
   }
   string cc;
   while(cin>>cc)
  {
    if(cc=="END") break;
    if(cc=="SHELVE")
    {
        while(!q.empty())
        {
        node ee=*q.begin();
        q.erase(ee);
        set<node>::iterator it;
     // for(it=p.begin();it!=p.end();++it) cout<<*it.name<<endl;
        it=p.lower_bound(ee);
        if(it==p.begin()) cout<<"Put \""<<ee.name<<"\" first"<<endl;
        else
        {
         if(it==p.end()) it--;                           //这里出错,lower_bound()函数是返回一个大于等于value的值的位置,无论如何,it都应该--
         cout<<"Put \""<<ee.name<<"\" after \""<<(*it).name<<\"<<endl;
         p.insert(ee);                                      //这一句位置出错,不管书架上原先有没有书,都应该插入新还的书
        }
        }
    }
    else
    {
     string c;
     getline(cin,c);
     c=fun(c);
     node nn;
     nn.name=c;
  //   cout<<c<<endl;
     nn.author=mp[c];
  //   cout<<mp[c]<<endl;
     if(cc=="BORROW") p.erase(nn);   //从书架上拿书
     if(cc=="RETURN") q.insert(nn); //待处理的书
    }
  }
    return 0;
}




AC代码:
#include"iostream"
#include"cstring"
#include"sstream"
#include"set"
#include"map"
using namespace std;

struct node{
string name,author;
friend bool operator <(node a,node b){
if(a.author==b.author)
    return a.name<b.name;
else
    return a.author<b.author;
}
};

set<node> p;   //存放书架上原有书籍
set<node> q;   //存放借阅和归还信息
map<string,string> mp;

string fun(string c)
{
for(int i=0;i<c.size();i++)
{
    if(c[i]==\") c[i]=‘ ‘;
}
string cc,rev;
stringstream ss(c);
int f=0;
while(ss>>cc)
{
    if(f++) rev+=‘ ‘;
    rev+=cc;
}
return rev;
}

void deal(string temp)    //存好书架上的信息
{
    for(int i=0;i<temp.size();i++)
    {
        if(temp[i]==\") temp[i]=‘ ‘;
    }
    stringstream  ss(temp);
    string s;
    int flag=0,f=0;
    node e;
     while(ss>>s)
     {
         if(s=="by")
         {
            flag=1;
         }
         if(flag!=1)
         {
           if(f++) e.name+=‘ ‘;
           e.name+=s;
         }
         if(flag==1)
         {
             if(f++) e.author+=‘ ‘;
             e.author+=s;
         }
     }
     p.insert(e);
     mp[e.name]=e.author;
}


int main()
{
   string temp;
   while(getline(cin,temp))
   {
       if(temp=="END") break;
       deal(temp);
   }
   string cc;
   while(cin>>cc)
  {
    if(cc=="END") break;
    if(cc=="SHELVE")
    {
        while(!q.empty())
        {
        node ee=*q.begin();
        q.erase(ee);
        set<node>::iterator it;
     // for(it=p.begin();it!=p.end();++it) cout<<*it.name<<endl;
        it=p.lower_bound(ee);
        if(it==p.begin()) cout<<"Put \""<<ee.name<<"\" first"<<endl;
        else
        {
          it--;
         cout<<"Put \""<<ee.name<<"\" after \""<<(*it).name<<\"<<endl;
        
        } p.insert(ee);
        }
        cout<<"END"<<endl;

    }
    else
    {
     string c;
     getline(cin,c);
     c=fun(c);
     node nn;
     nn.name=c;
  //   cout<<c<<endl;
     nn.author=mp[c];
  //   cout<<mp[c]<<endl;
     if(cc=="BORROW") p.erase(nn);   //从书架上拿书
     if(cc=="RETURN") q.insert(nn); //待处理的书
    }
  }
    return 0;
}

 

Borrowers

标签:

原文地址:http://www.cnblogs.com/zsyacm666666/p/4659843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!