您的位置:首页 > 其它

UVA 230 Borrowers (STL运用)

2015-06-09 09:57 302 查看
Borrowers

Time Limit: 3000MS Memory Limit: Unknown64bit IO Format: %lld & %llu
Submit

Status

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:

``title" byauthor
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 ``title" first
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.ENDBORROW "Algorithms"BORROW "The C Programming Language"RETURN "Algorithms"RETURN "The C
Programming Language"SHELVEEND

Sample Output

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




能不能说 做这个题目的时候是真鸡巴日了狗了,都怪没看清楚题目意思 ,题目要求每次SHELVE 都要在最后输出“END”,但是偏偏 就没注意!让一个网友朋友来做 很快就做出来了,对照他的标程运行结果,才发现了那个问题,否则还真以为是逻辑bug、逻辑过程都注释了~



#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

struct Node
{
    string name;
    string title;
    Node(string a,string b)
    {
        name=b;
        title=a;
    }
    friend bool operator<(Node a,Node b)
    {
        if(a.name==b.name)
            return  a.title<b.title;
        return a.name<b.name;
    }
};

int main()
{
    string str;
    string aa("by");
    vector<Node>cnt;
    vector<Node>xyz;
    while(getline(cin,str))
    {
        if(str=="END")
            break;
        cnt.push_back(Node(str.substr(1,str.find(aa)-3),str.substr(str.find(aa)+3,str.size()-str.find(aa)-3)));
    }
    string str2;
    xyz=cnt;
    vector<Node>dict;
    while(getline(cin,str2))
    {
        string cmd1,cmd2;

        cmd1=str2.substr(0,6);
        if(str2.size()>6)
            cmd2=str2.substr(8,str2.size()-9);
        if(cmd1=="END")
            break;
        if(cmd1[0]=='B')//借走就要删除
        {
            for(vector<Node>::iterator it=cnt.begin(); it!=cnt.end();)
            {
                if((*it).title==cmd2)
                {
                    cnt.erase(it);
                    break;
                }
                else
                    it++;
            }

        }

        else if(cmd1[0]=='R')
        {
            for(vector<Node>::iterator it=xyz.begin(); it!=xyz.end(); it++)
            {
                if((*it).title==cmd2)
                {
                    dict.push_back(*it);//还得书保存在dict
                }
            }
        }
        else
        {
            sort(cnt.begin(),cnt.end());
            sort(dict.begin(),dict.end());

            for(vector<Node>::iterator t=dict.begin(); t!=dict.end(); )//对于每一本要还回来的书
            {//都要寻找她们的位置。

                if(cnt.size()==0)//放在开头
                {
                    cout<<"Put "<<'\"'<<(*t).title<<'\"'<<" first"<<endl;
                    cnt.push_back(*t);
                    dict.erase(t);

                    continue;

                }
                sort(cnt.begin(),cnt.end());
                int ok=0;
                vector<Node>::iterator i;
                for(i=cnt.begin(); i!=cnt.end(); i++)
                {
                    if((*i)<(*t))
                    {
                        continue;
                    }
                    else//放在中间
                    {
                        ok=1;
                        if(i==cnt.begin())
                        {
                            cout<<"Put "<<'\"'<<(*t).title<<'\"'<<" first"<<endl;
                            cnt.push_back(*t);
                            dict.erase(t);
                            break;
                        }
                        cout<<"Put "<<'\"'<<(*t).title<<'\"'<<" after "<<'\"'<<(*(--i)).title<<'\"'<<endl;
                        cnt.push_back(*t);
                        dict.erase(t);
                        break;
                    }
                }
                if(!ok)//放在末尾
                {
                    cout<<"Put "<<'\"'<<(*t).title<<'\"'<<" after "<<'"'<<(*(--i)).title<<'\"'<<endl;
                    cnt.push_back(*t);
                    dict.erase(t);
                }
                sort(cnt.begin(),cnt.end());
            }
            cout<<"END"<<endl;//最后一定要有“END”
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: