习题5_8 图书管理系统(Borrowers, ACM/ICPC World Finals 1994, UVa230)
Posted As_zyh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了习题5_8 图书管理系统(Borrowers, ACM/ICPC World Finals 1994, UVa230)相关的知识,希望对你有一定的参考价值。
题目描述:
你的任务是模拟一个图书管理系统。首先输入若干图书的标题和作者(标题各不相同,以END结束),然后是若干指令:BORROW指令表示借书,RETURN指令表示还书,SHELVE指令表示把所有已归还但还未上架的图书排序后一次插入书架并输出图书标题和插入位置(可能是第一本书或者某本书的后面,也可能是最前面)。
图书排序的方法是按作者从小到大排,再按标题从小到大排。在处理第一条指令之前,你应当将所有图书按照这种方式排列。
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<sstream>
using namespace std;
set<pair<string, string> > lbr; // first is writer, second is book ,图书馆所有书
map<string, int> IDcache; // 把书名映射成ID书号(按顺序)
vector<string> BOOKcache; // 根据ID取书
set<int> in_lbr; // 图书馆现存的书
set<int> borrowed; // 被借出的书
set<int> back; // 已归还未上架的书
int main()
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string line;
while(getline(cin, line))
if(line[0] == 'E') break;
string book, writer, s;
stringstream ss(line);
int cnt = 0;
while(ss >> s)
if(s == "by") break;
if(++cnt == 1) book += s;
else book = book + " " + s;
while(ss >> s)
writer += s;
lbr.insert(make_pair(writer, book));
int idx = 0;
for(set<pair<string, string> >::iterator it = lbr.begin(); it != lbr.end(); it++)
IDcache[(*it).second] = idx++;
BOOKcache.push_back((*it).second);
in_lbr.insert(IDcache[(*it).second]);
while(getline(cin, line))
if(line[0] == 'E') break;
string cmd, s;
stringstream ss(line);
ss >> cmd;
int cnt = 0;
if(cmd[0] == 'B')
string book;
while(ss >> s)
if(++cnt == 1) book += s;
else book = book + " " + s;
int id = IDcache[book];
in_lbr.erase(id);
borrowed.insert(id);
else if(cmd[0] == 'R')
string book;
while(ss >> s)
if(++cnt == 1) book += s;
else book = book + " " + s;
int id = IDcache[book];
borrowed.erase(id);
back.insert(id);
else if(cmd[0] == 'S')
for(set<int>::iterator it = back.begin(); it != back.end(); it++)
cout << "Put " << BOOKcache[*it];
in_lbr.insert(*it);
set<int>::iterator it_in_lbr;
for(set<int>::iterator it_2 = in_lbr.begin(); it_2 != in_lbr.find(*it); it_2++)
it_in_lbr = it_2;
if(*it != *in_lbr.begin()) cout << " after " << BOOKcache[*it_in_lbr] << "\\n";
else cout << " first\\n";
cout << "END\\n";
back.clear();
return 0;
以上是关于习题5_8 图书管理系统(Borrowers, ACM/ICPC World Finals 1994, UVa230)的主要内容,如果未能解决你的问题,请参考以下文章