Borrowers
Description
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" byauthor
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 这道题完全是借鉴别人博客上的,不过当时没有理解深刻,所以犯了几个小错误
WA代码:
#include"iostream"
#include"cstring"
#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;
else
return a.author<b.author;
}
}; set<node> p; //存放书架上原有书籍
set<node> q; //存放借阅和归还信息
map<string,string> mp; string fun(string c)
{
for(int i=0;i<c.size();i++)
{
if(c[i]=='\"') c[i]=' ';
}
string cc,rev;
stringstream ss(c);
int f=0;
while(ss>>cc)
{
if(f++) rev+=' ';
rev+=cc;
}
return rev;
} void deal(string temp) //存好书架上的信息
{
for(int i=0;i<temp.size();i++)
{
if(temp[i]=='\"') temp[i]=' ';
}
stringstream ss(temp);
string s;
int flag=0,f=0;
node e;
while(ss>>s)
{
if(s=="by")
{
flag=1;
}
if(flag!=1)
{
if(f++) e.name+=' ';
e.name+=s;
}
if(flag==1)
{
if(f++) e.author+=' ';
e.author+=s;
}
}
p.insert(e);
mp[e.name]=e.author;
} int main()
{
string temp;
while(getline(cin,temp))
{
if(temp=="END") break;
deal(temp);
}
string cc;
while(cin>>cc)
{
if(cc=="END") break;
if(cc=="SHELVE")
{
while(!q.empty())
{
node ee=*q.begin();
q.erase(ee);
set<node>::iterator it;
// for(it=p.begin();it!=p.end();++it) cout<<*it.name<<endl;
it=p.lower_bound(ee);
if(it==p.begin()) cout<<"Put \""<<ee.name<<"\" first"<<endl;
else
{
if(it==p.end()) it--; //这里出错,lower_bound()函数是返回一个大于等于value的值的位置,无论如何,it都应该--
cout<<"Put \""<<ee.name<<"\" after \""<<(*it).name<<'\"'<<endl;
p.insert(ee); //这一句位置出错,不管书架上原先有没有书,都应该插入新还的书
}
}
}
else
{
string c;
getline(cin,c);
c=fun(c);
node nn;
nn.name=c;
// cout<<c<<endl;
nn.author=mp[c];
// cout<<mp[c]<<endl;
if(cc=="BORROW") p.erase(nn); //从书架上拿书
if(cc=="RETURN") q.insert(nn); //待处理的书
}
}
return 0;
} AC代码:
#include"iostream"
#include"cstring"
#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;
else
return a.author<b.author;
}
}; set<node> p; //存放书架上原有书籍
set<node> q; //存放借阅和归还信息
map<string,string> mp; string fun(string c)
{
for(int i=0;i<c.size();i++)
{
if(c[i]=='\"') c[i]=' ';
}
string cc,rev;
stringstream ss(c);
int f=0;
while(ss>>cc)
{
if(f++) rev+=' ';
rev+=cc;
}
return rev;
} void deal(string temp) //存好书架上的信息
{
for(int i=0;i<temp.size();i++)
{
if(temp[i]=='\"') temp[i]=' ';
}
stringstream ss(temp);
string s;
int flag=0,f=0;
node e;
while(ss>>s)
{
if(s=="by")
{
flag=1;
}
if(flag!=1)
{
if(f++) e.name+=' ';
e.name+=s;
}
if(flag==1)
{
if(f++) e.author+=' ';
e.author+=s;
}
}
p.insert(e);
mp[e.name]=e.author;
} int main()
{
string temp;
while(getline(cin,temp))
{
if(temp=="END") break;
deal(temp);
}
string cc;
while(cin>>cc)
{
if(cc=="END") break;
if(cc=="SHELVE")
{
while(!q.empty())
{
node ee=*q.begin();
q.erase(ee);
set<node>::iterator it;
// for(it=p.begin();it!=p.end();++it) cout<<*it.name<<endl;
it=p.lower_bound(ee);
if(it==p.begin()) cout<<"Put \""<<ee.name<<"\" first"<<endl;
else
{
it--;
cout<<"Put \""<<ee.name<<"\" after \""<<(*it).name<<'\"'<<endl; } p.insert(ee);
}
cout<<"END"<<endl; }
else
{
string c;
getline(cin,c);
c=fun(c);
node nn;
nn.name=c;
// cout<<c<<endl;
nn.author=mp[c];
// cout<<mp[c]<<endl;
if(cc=="BORROW") p.erase(nn); //从书架上拿书
if(cc=="RETURN") q.insert(nn); //待处理的书
}
}
return 0;
}
Borrowers的更多相关文章
- uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...
- UVa230 Borrowers (STL)
Borrowers I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...
- [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers
//又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...
- UVa 230 Borrowers(map和set)
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ...
- Borrowers UVA - 230
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...
- UVa230 Borrowers
原题链接 UVa230 思路 这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的 ...
- Uva - 230 - Borrowers
AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...
- UVA 230 Borrowers (STL 行读入的处理 重载小于号)
题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...
- 【习题 5-8 UVA - 230】Borrowers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...
随机推荐
- Luogu P1144 最短路计数 【最短路】 By cellur925
题目传送门 常规的最短路计数问题:注意有重边(重边不用理,看样例),自环(读入时过滤). 另外这个无向图没有权,其实可以直接bfs做,但考虑到以后带权的情况,按spfa走了. 水题被卡了三次(嘤嘤嘤 ...
- 用jdbc连接数据库并简单执行SQL语句
一:版本一.这种存在一个问题就是每执行一次操作都会创建一次Connection链接和且释放一次链接 1:创建pojo对象(OR映射,一个pojo类对应一张数据库表) package com.yin ...
- iOS导航栏NavigationBar的颜色,按钮和标题以及字体颜色
首先,层级关系: leftBarButtonItem.rightBarButtonItem.title都是加在UINavigationItem上的,UINavigationItem再加在Navigat ...
- jmeter(六)关联
话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一些,今天就关联来讲解一下. JMeter的关联方法有两种:后置处理器-正则表达式 ...
- sdut1642Simple Arithmetics(模拟)
链接 发个长长的模拟 这题要注意的地方挺多 -的个数 以及对齐的情况 全都注意好了 大数的加减乘就可以了 #include <iostream> #include<cstdio> ...
- Webform 内置对象 Session对象、Application全局对象,ViewState
Session 每台电脑访问服务器,都有独立的session,key值都一样,内容不一样. 1.session保存在服务器上. 2.session没有持久性,保存周期就是20分钟. 重点: sessi ...
- Windows远程桌面连接复制文件失败或非常慢
环境搭建过程中经常会遇到需要将文件从本机传到云服务器或者企业内部服务器上的场景,此时如果文件过大的话要传个半天或者直接告诉你复制失败,让人好生无奈 ~ ~. windows环境下,可以将本地磁盘映 ...
- JS中的对象之原型
对象 ECMAScript做为一个高度抽象的面向对象语言,是通过_对象_来交互的.即使ECMAScript里边也有_基本类型_,但是,当需要的时候,它们也会被转换成对象. 一个对象就是一个属性集合,并 ...
- iOS-控件响应用户控制事件之事件处理
事件处理 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication.UIViewControlle ...
- vba根据部门分别汇总不同部门下的人员不同培训内容的时长总计,多条件求和
Option Explicit Sub yy() Dim d, arr, s$, i&, m&, w$ Set d = CreateObject("Scripting.Dic ...