IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 算法竞赛入门经典 习题5-8 -> 正文阅读

[开发测试]算法竞赛入门经典 习题5-8

UVa230

Borrowers

图书馆书架上有一些已经排序(首先按照作者升序排序、然后按照书名升序排序)的书,使用程序来模拟借书和还书的过程。

在输入SHELVE指令时,需要将已经RETURN的书按照已经排序的顺序输出其在书架上的位置,这就要求对已经RETURN的书也进行排序,同时书架上的被BORROW的书会影响需要上架的书的位置,因此创建shelfborrowedreturned三个书架会比较方便。

uDebug上有两个超长的测试用例过不去,但是提交到UVa上也AC了。

#include <iostream>
#include <set>
#include <string>

using namespace std;

struct Book
{
	string title;
	string author;
	Book(const string &title, const string &author)
		: title(title), author(author) {};
	bool operator<(const Book &book) const
	{
		if (author < book.author) return true;
		else if (author == book.author) return title < book.title;
		else return false;
	}
};

set<Book>::iterator SearchBookByTitle(set<Book> &shelf, const string &title)
{
	for (auto iter = shelf.begin(); iter != shelf.end(); iter++)
	{
		if (iter->title == title) {
			return iter;
		}
	}
	return shelf.end();
}

int main()
{
	string input;
	size_t pos;
	set<Book> shelf, borrowed, returned;
	while (getline(cin, input)) {
		if (input == "END") break;
		pos = input.find('"', 1);
		Book book(input.substr(0, pos + 1), input.substr(pos + 5));
		shelf.insert(book);
	}
	while (getline(cin, input)) {
		if (input == "END") break;
		pos = input.find(' ');
		string cmd(input.substr(0, pos)), title(input.substr(pos + 1));
		if (cmd[0] == 'B') {
			set<Book>::iterator iter = SearchBookByTitle(shelf, title);
			if (iter != shelf.end()) {
				borrowed.insert(*iter);
				shelf.erase(iter);
			}
		}
		else if (cmd[0] == 'R') {
			set<Book>::iterator iter = SearchBookByTitle(borrowed, title);
			if (iter != borrowed.end()) {
				returned.insert(*iter);
				borrowed.erase(iter);
			}
		}
		else if (cmd[0] == 'S') {
			for (const Book &book : returned)
			{
				cout << "Put " << book.title;
				auto iter = shelf.lower_bound(book);
				if (iter == shelf.begin()) {
					cout << " first" << endl;
				}
				else {
					cout << " after " << (--iter)->title << endl;
				}
				shelf.insert(book);
			}
			returned.clear();
			cout << "END" << endl;
		}
		else;
	}
	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
*/

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-07-24 11:47:47  更:2021-07-24 11:48:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/24 1:53:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码