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经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
随机推荐
- 【python学习笔记01】python的数据类型
python的基本数据类型 整型 int 浮点型 float 真值 bool 字符串 str 列表 list #[1,2,3] 元组 tuple #(1,2,3) 字典 dict ...
- 【2】开发环境的搭建,Ubuntu14.04
这里使用的是Ubuntu14.04 Unity 更新源 首先,将更新源更换为国内更新源,我这里使用的是网易的更新源 sudo gedit /etc/apt/sources.list deb http: ...
- SQL Server 2008空间数据应用系列七:基于Bing Maps(Silverlight) 的空间数据展现
原文:SQL Server 2008空间数据应用系列七:基于Bing Maps(Silverlight) 的空间数据展现 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft ...
- Linux系统编程(13)——Shell的基本语法
按照惯例,Shell变量由全大写字母加下划线组成,有两种类型的Shell变量:环境变量和本地变量. 环境变量: 环境变量可以从父进程传给子进程,因此Shell进程的环境变量可以从当前Shell进程传给 ...
- Xcopy参数介绍
DOS批处理命令,永远是不朽的命令,不仅功能强大,同时,速度也是最快的!但是,很多新手学习计算机,都已经遗忘了本不该忘记的批处理命令. 我们不可数典忘祖,该学习的还是要学习,不该忘记的还是不能忘记,尤 ...
- Intersection of Two Linked Lists 解答
Question Write a program to find the node at which the intersection of two singly linked lists begin ...
- openstack API debug OpenstackEveryProject_CLI,curl_based
1,基于Openstack 每个服务组件client客户端,eg,nova 客户端软件包名称是python-novaclient, 别的都一样,把python-novaclient (nova替换成组 ...
- ObjectOutputStream 追加写入读取错误 - 自己的实现方案
本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015 问题描述.问题出现的原因.尝试解决办法,请参见鄙人上一编博客. 上一编文章解 ...
- 改变UITableViewCell按下去的颜色
UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...
- 浅谈ThreadPool 线程池
本文来自:http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html 相关概念: 线程池可以看做容纳线程的容器: 一个应用程序最多只能有 ...