【习题 5-8 UVA

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

用map+set写个模拟就好。 3个区域 书架、桌子、别人的手上。 其中前两个区域的书都能借出去。

【代码】

#include <bits/stdc++.h>
using namespace std;

set <pair<string, string> > mset1,mset2,mset3;
map<string, string> author;
string s1,s2,s3;

int main()
{
	//freopen("F:\rush.txt", "r", stdin);
	while (getline(cin,s1))
	{
		if(s1 == "END") break;
		s2 = "";
		int now = 1;
		while (s1[now] != '"') s2 += s1[now++];
		assert(s1[now] == '"');
		now+=5;
		s3 = s1.substr(now);
		mset1.insert(make_pair(s3, s2));
		author[s2] = s3;
	}
	while (getline(cin, s1))
	{
		if (s1 == "END") break;
		if (s1[0] == 'B')
		{
			s2 = s1.substr(8);
			s2.erase((int)s2.size() - 1);
			auto it = mset1.find(make_pair(author[s2], s2));
			if (it != mset1.end())
			{
				mset3.insert((*it));
				mset1.erase(it);
			}
			else {
				it = mset2.find(make_pair(author[s2], s2));
				if (it != mset2.end())
				{
					mset3.insert((*it));
					mset2.erase(it);
				}
			}
		}
		else 
			if (s1[0] == 'R')
			{
				s2 = s1.substr(8);
				s2.erase((int)s2.size() - 1);
				auto it = mset3.find(make_pair(author[s2], s2));
				if (it != mset3.end())
				{
					mset2.insert((*it));
					mset3.erase(it);
				}
			}
			else {
				for (auto temp : mset2)
				{
					mset1.insert(temp);
					auto it = mset1.find(temp);
					if (it != mset1.begin())
					{
						it--;
						cout << "Put " << '"' << temp.second << '"' << " after " << '"' << (*it).second << '"' << endl;
					}
					else
						cout << "Put "<<'"'<<temp.second << '"'<<" first" << endl;
				}
				cout << "END" << endl;
				mset2.clear();
			}

	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7671009.html