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:

``titlebyauthor

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 ``titlefirst

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的更多相关文章

  1. uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!

    I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...

  2. UVa230 Borrowers (STL)

     Borrowers  I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...

  3. [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers

    //又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...

  4. UVa 230 Borrowers(map和set)

    I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ...

  5. Borrowers UVA - 230

      I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...

  6. UVa230 Borrowers

    原题链接 UVa230 思路 这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的 ...

  7. Uva - 230 - Borrowers

    AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...

  8. UVA 230 Borrowers (STL 行读入的处理 重载小于号)

    题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...

  9. 【习题 5-8 UVA - 230】Borrowers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...

随机推荐

  1. 开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等

    开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等 C++开发网络通信程序时用asio是个不错的选择 ...

  2. jacoco 的使用及与jenkins的集成

    1.把jacocoagent.jar的包放入到dockerfile COPY jacocoagent.jar /opt/jacoco/lib/jacocoagent.jar 2.打完镜像,需要启动容器 ...

  3. 递推DP UVA 607 Scheduling Lectures

    题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...

  4. jmeter(一)工具介绍(二)

    1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...

  5. ssm基础配置

    1.导包 <dependencies> <dependency> <groupId>org.springframework</groupId> < ...

  6. Java操作pdf: JarsperReport的简单使用

    在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...

  7. ScrollView属性

    1.文本内容过长,一个屏幕显示不下,这时候就把显示文本的 TextView包裹在ScrollView里面,可以做到滚动下滑查看的功能 2.隐藏滚动条 标签属性设置android:scrollbars= ...

  8. 微信小程序组件解读和分析:七、button按钮

    button按钮组件说明: button,顾名思义,按钮,类似于html的button标签.我们可以设置按钮的属性,比如字体颜色大小,背景颜色等,可以给按钮绑定事件,用户点击时会触发事件. butto ...

  9. 迅为i.MX6Q嵌入式开发板

    工业级核心板:核心板10层高速PCB设计,充分保证电磁兼容. 01. 处理器:开发板默认是四核商业扩展级芯片,可根据用户需求更换单核.双核.工业级.汽车级处理器,批量更省成本. 02. 扩展引脚:32 ...

  10. 迅速搞懂JavaScript正则表达式之方法

    咱们来看看JavaScript中都有哪些操作正则的方法. RegExp RegExp 是正则表达式的构造函数. 使用构造函数创建正则表达式有多种写法: new RegExp('abc');// /ab ...