UVa 230 Borrowers(map和set)
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 " title1 " after " title2 "
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"
SHELVEEND
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
题意
模拟图书馆借书还书(按作者字典序从小到大,再按书名从小到大),BRROW借一本书,RETURN还一本书,SHELVES把书按序一本本放回书架
题解
由于借书还书只给你书名,所以需要用map<string,string>把书名映射到作者,这样才可以用set查找
BRROW借书:就是删除erase一本书的作者和书名
RETURN还书:就是把作者和书名放进set1中
SHELVES放回书架:就是把遍历所有已经还的书it,用lower-bound找到插入后的位子hit,如果当前set为空或者hit==set.begin()就输出first,否则输出hit前面1个(hit--),最后插入*it即可,别忘了最后输出END
代码
#include<bits/stdc++.h>
using namespace std;
struct book
{
string title,author;
book(string t,string a):title(t),author(a){}
bool operator <(const book &rhs)const
{
return author<rhs.author||(author==rhs.author&&title<rhs.title);
}
};
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
string s;
map<string,string> ma;
set<book> se,se1;
while(getline(cin,s))
{
if(s=="END")break;
int pos=s.find(" by ");
ma[s.substr(,pos)]=s.substr(pos+);
se.insert(book(s.substr(,pos),s.substr(pos+)));
}
while(getline(cin,s))
{
if(s[]=='E')break;
else if(s[]=='S')
{
set<book>::iterator it,bit;
for(it=se1.begin();it!=se1.end();it++)
{
bit=se.lower_bound(*it);
cout<<"Put "<<it->title;
if(se.empty()||bit==se.begin())
cout<<" first"<<endl;
else
cout<<" after "<<(--bit)->title<<endl;
se.insert(*it);
}
se1.clear();
cout<<"END"<<endl;
}
else if(s[]=='B')
se.erase(book(s.substr(),ma[s.substr()]));
else if(s[]=='R')
se1.insert(book(s.substr(),ma[s.substr()]));
}
return ;
}
UVa 230 Borrowers(map和set)的更多相关文章
- uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
		
I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...
 - Uva - 230 - Borrowers
		
AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...
 - UVA 230 Borrowers (STL 行读入的处理 重载小于号)
		
题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...
 - Borrowers UVA - 230
		
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...
 - 【习题 5-8 UVA - 230】Borrowers
		
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...
 - UVA 156 Ananagrams ---map
		
题目链接 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列( ...
 - uva 156 (map)
		
暑假培训习题 1.用vector<string>储存string类型的输入单词: 2.将vector中的元素逐一标准化后映射进map中,并给map值加一: 3.新建一个空的vector 4 ...
 - UVA 12035 War Map
		
让我先把微笑送给出题人 这个题最基础的一个想法:先找出一个度数和为总度数和的1/2的点集,然后判断这个点集和这个点集的补集能否形成二分图.但是就算我们把判断的复杂度看成O(1),这个算法的复杂度也是 ...
 - UVa 1592 Database (map)
		
题意:给出n行m列的数据库(数据范围: n 1~10000, m 1~10), 问你能不能找出两行r1, r2,使得这两行中的c1, c2列是一样的, 即(r1,c1)==(r2,c1) && ...
 
随机推荐
- ubuntu  查看系统是32位还是64位
			
查看cpu信息 cat /proc/cpiinfo 查看ubuntu版本: cat /etc/issue 查看系统是32位还是64位 方法1: #查看long的位数,返回32或64 getconf L ...
 - 绕过限制,在PC上调试微信手机页面
			
场景 假设一个手机页面,开发者对其做了限制,导致只能在微信客户端中打开.而众所周知手机上非常不利于调试页面,所以需要能在电脑上打开并进行调试.这里针对常见的三种页面做一下分析,一一绕过其限制,(当然不 ...
 - [UE4]有限状态机、动画状态机、纯函数
			
有限状态机 FSM:Finite State Machine,表示有限个状态以及在这些状态之间转移和动作的数学模型 纯函数: 纯函数: 先后调用顺序不重要,没有修改任何数值,只是获取数值或者临时计算一 ...
 - 激活函数sigmoid、tanh、relu、Swish
			
激活函数的作用主要是引入非线性因素,解决线性模型表达能力不足的缺陷 sigmoid函数可以从图像中看出,当x向两端走的时候,y值越来越接近1和-1,这种现象称为饱和,饱和意味着当x=100和x=100 ...
 - 管理oracle 11g RAC 常用命令
			
1).检查集群状态: [grid@rac02 ~]$ crsctl check cluster CRS-4537: Cluster Ready Services is online CRS-4529: ...
 - Oauth2.0(六):Resource Owner Password Credentials 授权和 Client Credentials 授权
			
这两种简称 Password 方式和 Client 方式吧,都只适用于应用是受信任的场景.一个典型的例子是同一个企业内部的不同产品要使用本企业的 Oauth2.0 体系.在有些情况下,产品希望能够定制 ...
 - 显式锁(四)Lock的等待通知机制Condition
			
任意一个Java对象,都拥有一组监视器方法(定义在根类Object上),主要包括:wait( ).wait(long timeout).notify().notifyAll()方法:这些方法与关 ...
 - python中的模块及包及软件目录结构规范
			
知识内容: 1.模块的定义与分类 2.模块的导入 3.模块与包 4.不同目录下的模块调用 一.模块的定义与分类 1.什么是模块 模块就是实现了某个功能的代码集合,模块是由一大堆代码构成的 类似于函数式 ...
 - oracle一个用户操作多个表空间中表的问题
			
首先,授权给指定用户. 一个用户的默认表空间只能有一个,但是你可以试下用下面的语句为其授权在别的表空间中创建对像: alter user username quota 0||unlimited on ...
 - Python简单实现基于VSM的余弦相似度计算
			
在知识图谱构建阶段的实体对齐和属性值决策.判断一篇文章是否是你喜欢的文章.比较两篇文章的相似性等实例中,都涉及到了向量空间模型(Vector Space Model,简称VSM)和余弦相似度计算相关知 ...