题意:你的任务是模拟一个图书管理系统。首先输入若干图书的标题和作者(标题各不相同,格式为:(“书名” by 作者. ),以END结束),然后是若干指令:BORROW指令表示借书,RETURN指令表示还书,SHELVE指令表示吧所有已归还还未上架的图书排序后一次插入的书架并输出图书标题和插入位置(可能是第一本书或者某本输的后面),每个SHELVE指令结束输出一个"END"。图书排序的方法是先按照作者从小大到排序,再按标题从小到大排。在处理第一条指令之前,你应当先将所有图书按照这种方式排序。
思路:用string的substr函数分离书名和作者,操作和书名。用一个set或者map保存书的清单,然后模拟下面的指令,用一个set保存已经归还还未上架的书,用一个set表示书架上现在有的书。set自带的lower_bound查找插入位置。
map保存书的信息:
#include<bits/stdc++.h>
using namespace std;
struct Book
{
string name, author;
bool operator < (const Book& a) const
{
return author < a.author || (author == a.author && name < a.name);
}
}book;
unordered_map<string, string> List;//图书列表,书名对应作者
set<Book> lib, R;//图书馆剩余的书, 归还的书
void print()
{
for (auto i: R)
{
auto j = lib.lower_bound(i); //查找插入位置
if (j == lib.begin()) cout << "Put " << i.name << " first" << endl;
else { cout << "Put " << i.name << " after " << (*prev(j)).name << endl; }//prev()获得前一个迭代器
lib.insert(i);
}
cout << "END" << endl;
R.clear();
}
int main()
{
string str;
while (getline(cin,str) && str[0] != 'E')
{
book.name = str.substr(0, str.find_last_of('\"') + 1);//书名
book.author = str.substr(str.find(" by ") + 4);//作者
List[book.name] = book.author;//保存书的清单
lib.insert(book);
}
while (getline(cin,str) && str[0] != 'E')
{
if (str[0] == 'S') { print(); continue; }//输出
string name = str.substr(7);//书名
book.name = name; book.author = List[book.name];
if (str[0] == 'B') lib.erase(book);//借书
else if (str[0] == 'R') R.insert(book);//还书
}
return 0;
}
/*
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
*/
set保存书的信息:
#include<bits/stdc++.h>
using namespace std;
struct Book
{
string name, author;
bool operator < (const Book& a) const
{
return author < a.author || (author == a.author && name < a.name);
}
}book;
set<Book> List, lib, R;//图书列表, 图书馆剩余的书, 归还的书
void print()
{
for (auto i: R)
{
auto j = lib.lower_bound(i); //查找插入位置
if (j == lib.begin()) cout << "Put " << i.name << " first" << endl;
else { cout << "Put " << i.name << " after " << (*prev(j)).name << endl; }//prev()获得前一个迭代器
lib.insert(i);
}
cout << "END" << endl;
R.clear();
}
int main()
{
string str;
while (getline(cin,str) && str[0] != 'E')
{
book.name = str.substr(0, str.rfind('\"') + 1);//书名
book.author = str.substr(str.rfind(" by "));//作者
List.insert(book);
}
lib = List;//图书馆现在有的书
//for (auto i: lib) cout << i.name << " by " << i.author << endl;
while (getline(cin,str) && str[0] != 'E')
{
if (str[0] == 'S') { print(); continue; }//输出
string name = str.substr(7);//书名
//cout << name << endl;
if (str[0] == 'B')//借书
{
for(auto i: lib)
{
if (i.name == name) { book = i; break; }
}
lib.erase(book);
}
else if (str[0] == 'R')//还书
{
for(auto i: List)
{
if (i.name == name) R.insert(i);
}
}
}
return 0;
}
/*
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
*/