题目地址: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;
}