uva 230 Borrowers

原题:
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:
title” by author
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 title 1 after title 2
or, for the special case of the book being the first in the collection:
Put title firstAfter 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

中文:
你的任务是模拟一个图书管理系统。首先输入若干图书的标题和作者(标题各不相同,以END结束),然后是若干指令:BORROW指令表示借书,RETURN指令表示还书,SHELVE指令表示把所有已归还但还未上架的图书排序后依次插入书架并输出图书标题
和插入位置(可能是第一本书或者某本书的后面)。
图书排序的方法是先按作者从小到大排,再按标题从小到大排。在处理第一条指令之前,你应当先将所有图书按照这种方式排序。

#include <bits/stdc++.h>
using namespace std;
struct book
{
    string name,author;
};
int cmp(const book &s1,const book &s2)
{
    if(s1.author!=s2.author)
        return s1.author<s2.author;
    else
        return s1.name<s2.name;
}
vector<book> vb;
map<string,int> msi;
int main()
{
    ios::sync_with_stdio(false);
    string tmp,tt;
    vb.clear();
    msi.clear();
//  fstream in,out;
//  in.open("in.txt");
//  out.open("out.txt");
    while(getline(cin,tmp))
    {
        if(tmp=="END")
            break;
        int ind=tmp.find_last_of('\"');
        string name;
        name.append(tmp.begin(),tmp.begin()+ind+1);
        string author;
        author.append(tmp.begin()+ind+5,tmp.end());
        book t;
        t.name=name,t.author=author;
        vb.push_back(t);
        msi[t.name]=1;
    }
    sort(vb.begin(),vb.end(),cmp);
    while(cin>>tmp)
    {
        if(tmp=="END")
            break;
        if(tmp=="BORROW")
        {
            cin.get();
            getline(cin,tt);
            msi[tt]=0;
        }
        if(tmp=="RETURN")
        {
            cin.get();
            getline(cin,tt);
            msi[tt]=-1;
        }
        if(tmp=="SHELVE")
        {
            for(int i=0;i<vb.size();i++)
            {
                if(msi[vb[i].name]==-1)
                {
                    int j;
                    for(j=i;j>=0;j--)
                        if(msi[vb[j].name]==1)
                            break;
                    if(j>-1)
                        cout << "Put " << vb[i].name << " after " << vb[j].name << endl;
                    else
                        cout << "Put " << vb[i].name << " first" << endl;
                    msi[vb[i].name]=1;
                }
            }
            cout<<"END"<<endl;
        }
    }
//  in.close();
//  out.close();
    return 0;
}

解答:
lrj紫书上面的题目,题目倒是不难,不过代码要比较讲究,注意cin.get()吸收回车。建立一个map

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值