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 Post方式发起http请求 使用百度接口地理编码
import os import httplib import json import urllib baiduapi="api.map.baidu.com:80" src=&qu ...
- 宽带连接工具[bat]
功能概述: 本工具使用批处理编写,提供自动判断网络状态以决定断开或是连上网络,本月已用宽带时长,到月初自动清零.提供联网日志功能,可以记录下所有的连接或断开网络记录.如果连接失败,自动提示输入密码,特 ...
- 前端MVVM学习之KnockOut(一)
MVVM理解 MVVM即Model-View-viewModel,是微软WPF和MVP(Model-View-Presenter)结合发展演变过来的一种新型架构框架. MVVM设计模式有以下优点: ( ...
- setPixel抛出java.lang.IllegalStateException
原来,从ImageView里读取的Bitmap,或者加载drawable里的图片资源,返回的都是一个immutalbe的bitmap,所以不能用setpixels 必须采取类似Bitmap bmp = ...
- 八、桥接模式--结构模式(Structural Pattern)
桥梁模式:将抽象化(Abstraction)与实现化 (Implementation)脱耦,使得二者可以独立地变化. 桥梁模式类图: 抽象化(Abstraction)角色:抽象化给出的定义,并保存 一 ...
- Android高德地图开发具体解释
这段时间开发的时候用到了高德地图,对高德地图开发有心得体会,如今分享给大家.对我开发过百度地图的我来说,整体来说高德地图Demo,没有百度解说的具体 个人更偏向于使用百度地图,可是没办发,项目须要使用 ...
- python使用post登陆电子科大信息门户并保存登陆后页面
python使用post登陆电子科大信息门户并保存登陆后页面 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import HTMLParser impor ...
- POJ3026(BFS + prim)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10554 Accepted: 3501 Descri ...
- 解决cognos以远程db2数据库为数据源的连接失败问题
问题现象为使用远程的db2来创建数据源时,测试连接时不通,好多人都说是驱动问题,将db2cc.jar拷贝到某lib目录下,实验不通: 在看到某哥们的博客时最后提了一句,说需要将数据库catalog到本 ...
- Python常用模块(time, datetime, random, os, sys, hashlib)
time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...