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:

``titleby 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 ``  " 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
这是一个STL的题目,好久没写STL了,就写了一个这个题目,尽量用STL写的。。
注意空格和双引号;
 #include<iostream>
#include<algorithm>
#include<string>
#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;
return a.author<b.author;
}
};
set<node> q; //这个是图书馆的书架上面的书
set<node> p; //这个是图书馆准备被放上书架的书,就是别人刚还的书,还没来得及放上书架
map<string,string> Mp; //这是一个书名指向作者的映射
void deal(string s) // 把输入的作者和书名放入结构体
{
node e;
int len=s.size();
for(int i=;i<len;i++) if(s[i]=='\"') s[i]=' ';
stringstream ss(s);
string temp;
int flag=,flagch=;
while(ss >> temp)
{
if(temp=="by") {flag=;flagch=;continue;}
if(flagch==)
{
if(flag++) e.name+=" ";
e.name+=temp;
}
else
{
if(flag++) e.author+=" ";
e.author+=temp;
}
}
Mp[e.name]=e.author;
q.insert(e);
return;
}
string fun(string st) //fun() 函数用于去掉输入的引号
{
string rev;
int len=st.size();
for(int i=;i<len;i++) if(st[i]=='\"') st[i]=' ';
int flag=;
stringstream ss(st);
string temp;
while(ss >> temp)
{
if(flag++) rev+=" ";
rev+=temp;
}
return rev;
}
int main()
{
string temp;
int num=;
while(getline(cin,temp)) //输入,处理,然后把书放上书架
{
if(temp=="END") break;
deal(temp);
}
while(cin >> temp) //执行指令,
{
if(temp=="END") break;
else if(temp=="SHELVE") //把书放上书架
{
while(!p.empty())
{
node e=*p.begin();
p.erase(e);
set<node>::iterator it=q.lower_bound(e);
if(it==q.begin()) cout << "Put \"" << e.name << "\" first" << endl;
else
{
it--;
cout << "Put \"" << e.name << "\" after \"" << (*it).name << "\"" << endl;
}
q.insert(e);
}
cout << "END" << endl;
}
else //可以一起处理
{
string book;
getline(cin,book);
book=fun(book);
string author=Mp[book];
node e;
e.name=book;
e.author=author;
if(temp=="BORROW") q.erase(e);
if(temp=="RETURN") p.insert(e);
}
}
return ;
}

UVa230 Borrowers (STL)的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers

    //又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...

  2. UVa230 Borrowers

    原题链接 UVa230 思路 这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的 ...

  3. UVA 230 Borrowers (STL 行读入的处理 重载小于号)

    题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...

  4. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  5. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  6. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  7. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  8. C++ STL简述

    前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...

  9. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

随机推荐

  1. InetAddress类的使用

    1.1. 简介 IP地址是IP使用的32位(IPv4)或者128位(IPv6)位无符号数字,它是传输层协议TCP,UDP的基础.InetAddress是Java对IP地址的封装,在java.net中有 ...

  2. 【转】生产环境MySQL Server核心参数的配置

         ⑴ lower_case_table_names              ● 推荐理由                    GNU/Linux 平台,对数据库.表.存储过程等对象名称大小 ...

  3. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  4. ASP.NET MVC 4.0 学习6-Model Binding

    一,ViewData,ViewBag與TempData ASP.NET MVC架構中,通過繼承在Controller中的ViewData,ViewBag和TempData和View頁面進行資料的存取, ...

  5. Maven的使用--Eclipse在线安装Maven插件m2e

    我使用的Eclipse版本是3.7(Indigo) 通过Eclipse的help选项,点击“Install New Software...”弹出安装对话框, 点击add按钮,在Location里输入h ...

  6. SQLServer优化资料整理(一)

    查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 ...

  7. 什么是Elasticsearch

    一个采用Restfull API 标准的高扩展性和高可用性的实时数据分析的全文搜索工具 Elasticsearch 涉及到的一些概念: 1.Node(节点): 单个的装有Elasticsearch服务 ...

  8. UESTC_The Most Wonderful Competition CDOJ 56

    The Most Wonderful Competition Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB ...

  9. 【转】RTSP流理解

    rtsp是使用udp还是tcp,是跟服务器有关,服务器那边说使用udp,那就使用udp,服务器说使用tcp那就使用tcp rtsp客户端的创建: 1.建立TCP socket,绑定服务器ip,用来传送 ...

  10. Unity编辑器扩展Texture显示选择框

    学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=. Unity开发了组件Inspector视图扩展API,如果我们要写插 ...