习题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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值