UVA 230 Borrowers

题目地址:https://vjudge.net/problem/UVA-230

题意:模拟一个图书馆管理系统。首先输入若干图书的标题和作者(标题各不相同,以END结束),然后是若干指令:BORROW指令表示借书,RETURN指令表示还书,SHEKVE指令表示把所有已归还但还未上架的图书排序后一次插入书架并输出图书标题和插入位置(可能是第一本书或者某本书的后面)。图书排序的方法是先按照作者从小到大排序,然后按照标题从小到大排序。处理第一条指令前,你应当先将所有图书按照这种方式排序。(翻译来自紫书)

理解了题意就简单了,前提是懂得STL容器的有关运用,然后就是注意一下输入和输出。

#include <set>
#include <map>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
	string book, name;
	bool operator<(const struct node &x) const {
		if(name != x.name) return name < x.name;
		return book < x.book;
	}
}nbook;
string s;
set<node> Set;
set<node>Set1;
pair<string, string> pr;
map<string, string> mat;

int main() {
	while(getline(cin, s)) {
		if(s[0] == 'E') break;
		int p = s.find('"',1);
		nbook.name = s.substr(p);
		nbook.book = s.substr(0, p+1);
		Set.insert(nbook);
		pr.first = nbook.book;
		pr.second = nbook.name;
		mat.insert(pr);
	}
	while(getline(cin, s)) {
		if(s[0] == 'E') break;
		if(s[0] == 'S') {
			set<node>::iterator it, It;
			for(it = Set1.begin(); it != Set1.end(); it++) {
				It = Set.lower_bound((*it));
				cout << "Put " << (*it).book << " ";
				if(Set.empty() || it == Set.begin()) printf("first\n");
				else cout << "after " << (*(--It)).book << endl;
				Set.insert(*it);
			}
			Set1.clear();
			printf("END\n");
		}
		else {
			int p = s.find('"');
			string s1 = s.substr(p);
			nbook.book = s1;
			nbook.name = mat[s1];
			if(s[0] == 'B') Set.erase(nbook);
			else Set1.insert(nbook);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值