UVa230 Borrowers (STL)
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 `` " 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.
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)的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers
//又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...
- UVa230 Borrowers
原题链接 UVa230 思路 这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的 ...
- UVA 230 Borrowers (STL 行读入的处理 重载小于号)
题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++ STL简述
前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
随机推荐
- C语言--关键字 typedef
一.typedef 1.基本使用 1> typedef 在基本数据类型中的使用 typedef int MyInt; // 相当于给 int 起了一个别名 typedef MyInt MyInt ...
- Nginx的Upload上传模块
前段时间做一个项目,需要上传文件,差不多需要20M左右,普通用php处理会比较麻烦,经常超时,而且大量占用资源.于是搜索了下,决定用nginx的upload上传模块来处理. 你可以在这里:http:/ ...
- 常用两种数据交换格式之XML和JSON的比较
目前,在web开发领域,主要的数据交换格式有XML和JSON,对于XML相信每一个web developer都不会感到陌生: 相比之下,JSON可能对于一些新步入开发领域的新手会感到有些陌生,也可能你 ...
- javascript调试
今天,发现了一个之前从未注意的角落,相信能够大大提高自己写JS的速度.能够迅速发现错误. 例如,今天的加班中调试一个js错误发现的一个例子. 1.Google浏览器报的错 以上是google浏览器报的 ...
- 窗口变化相关消息 OnSize、OnSizing和OnGetMinMaxInfo
最近用到窗口变化的一些东西,遇到几个相关的消息函数,简要分析,作为备忘. 3个消息分别是:WM_SIZE.WM_SIZING.WM_GETMINMAXINFO:分别对应相应的处理函数:OnSize.O ...
- noip2014总结
noip总结 经过七周的停课,我们终于迎来了期盼已久的noip考试.在这一次的noip考试中,我们经历了很多,也收获了很多.当然这一次考试中也有很多值得总结的地方,特写此总结. 这一次考试考得还不错, ...
- 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。
为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracl ...
- structs 拦截器
首先,要跟大家道个歉,前一阵子为给客户个一个DEMO,忙得不可开交,所以很久没有更新Blog.提到这个DEMO我想顺便跟大家分享一下心得——如果大家希望快速开发,一个类似Struts 2这样的简单方便 ...
- PHP 超强过滤函数
PHP 超强过滤函数 你有每次要过滤的时候总是去翻曾经的过滤代码的时候么? 你有搜索过怎样防过滤,防攻击的PHP解决方法么? 你有对全然遵循'过滤输入,避免输出',Web界经典说辞么? 事实上 ...
- Struts2使用Interceptor实现权限控制的应用实例详解
Struts2使用Interceptor实现权限控制的应用实例详解 拦截器:是Struts2框架的核心,重点之重.因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的.少说了. ...