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经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
随机推荐
- 蘑菇街2015校招技术类笔试题A卷,回忆版(杭州站)
笔试时间:10月9号 下午 1.一串数据的最大递增序列,输出个数 例如 4,2, 6,3, 1,5, 最大递增序列为, 2,3, 5,输出3, 2.求两个整型数据集合的交集,尽可能少用时间. 假设两个 ...
- 小希的迷宫(HDU 1272 并查集判断生成树)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Bone Collector(ZeroOnebag)
Bone Collector Problem Description Many years ago , in Teddy’s hometown there was a man who was call ...
- JS中各种宽度、高度、位置、距离总结
1.window.screen 浏览器与屏幕的距离,screenX(screenLeft),screenY(screenTop) 2.window.scrollTo(x,y) 将纵向滚动条移动到相对于 ...
- Ubuntu14.0.4 64位 ADT 连接手机调试问题
1:使用 lsusb 命令查看USB 设备 y@y:~$ lsusbBus 001 Device 002: ID 8087:8000 Intel Corp. Bus 001 Device 001: I ...
- onclick事件
onclick = "func(this);"----------->传递element对象 onclick = "func(event);"------ ...
- Apache监控
Apache性能监控 http://www.cnblogs.com/fnng/archive/2012/11/11/2765463.html 要监控apache的性能,我们需要修改配置文件,允许查看a ...
- FFmpeg深入分析之零-基础 <第一篇>
FFmpeg是相当强大的多媒体编解码框架,在深入分析其源代码之前必须要有基本的多媒体基础知识,否则其源代码会非常晦涩难懂.本文将从介绍一些基本的多媒体只是,主要是为研读ffmpeg源代码做准备,比如一 ...
- 定时任务:Java中Timer和TimerTask的使用
java.util.Timer定时器,实际上是个线程,定时调度所拥有的TimerTasks. 一个TimerTask实际上就是一个拥有run方法的类,需要定时执行的代码放到run方法体内,TimerT ...
- LeeCode-Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...