题面
![](https://img-blog.csdnimg.cn/img_convert/b6cd934c293c2438e1b9e427920627e8.png)
思路
一道模拟题。读入数据方面可以利用好书名带引号的性质,读作者读到行末即可。vector模拟书架上所有书,每个书都用bool标注了当时是否在书架上,用一个优先队列储存放在书桌上的书。借书操作直接将vector里的该书的bool量改为false表明已被借走即可。还书操作即将还回去的书入队。输出操作,先找到队顶的书在书架上的位置,将其bool量改为true,再寻找此时书架上该书的前一本,输出,出队,重复执行直到队空(桌上的书全部放入书架)。
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;
struct book{
string title,author;
bool in_shelf;
bool operator==(const book a){
return (author==a.author&&title==a.title);
}
};
bool operator<(const book a,const book b){
if(a.author==b.author) return a.title<b.title;
return a.author<b.author;
}
bool operator>(const book a,const book b){
if(a.author==b.author) return a.title>b.title;
return a.author>b.author;
}
priority_queue<book,vector<book>,greater<book> > re_q;//归还但未上架的队列
vector<book> lib;//图书库
map<string,string> ti_au;//储存标题对应的作者
int find_title(book a){
int l=0,r=lib.size()-1,m;
while(l<=r){
m=(l+r)/2;
if(lib[m]==a) return m;
else if(lib[m]<a) l=m+1;
else if(lib[m]>a) r=m-1;
}
return -1;
}
int main(){
char c;
while(1){
scanf(" ");
book a;
char c;a.title="";a.author="";a.in_shelf=true;
c=getchar();
if(c=='E'){
string s1;
getline(cin,s1);
break;
}//判断终止
a.title+=c;
while(1){//读入书名
c=getchar();
a.title+=c;
if(c=='"') break;//终止
}
string s0;cin>>s0;scanf(" ");//读去by和空格
while(1){//读入作者
c=getchar();
if(c=='\n') break;
a.author+=c;
}
ti_au[a.title]=a.author;
// cout<<a.title<<" "<<a.author<<endl;
lib.push_back(a);
}
sort(lib.begin(),lib.end());
while(1){
string op;
cin>>op;
if(op[0]=='E'){
break;
}
if(op[0]=='B'){//取出
scanf(" ");
string name="";
getline(cin,name);
book a;a.title=name;a.author=ti_au[name];
int x=find_title(a);
lib[x].in_shelf=false;
}
if(op[0]=='R'){//归还
scanf(" ");
string name="";
getline(cin,name);
book a;a.title=name;a.author=ti_au[name];
int x=find_title(a);
re_q.push(lib[x]);
}
if(op[0]=='S'){
while(!re_q.empty()){
int x=find_title(re_q.top());
lib[x].in_shelf=true;
cout<<"Put "<<re_q.top().title;
bool ok=false;
for(int i=x-1;i>=0;i--){
if(lib[i].in_shelf){
cout<<" after "<<lib[i].title<<endl;
ok=true;
break;
}
}
if(!ok) cout<<" first\n";
re_q.pop();
}
cout<<"END\n";
}
}
return 0;
}