Borrowers UVA - 230
I mean your borrowers of books — those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes.
– (Charles Lamb, Essays of Elia (1823) ‘The Two Races of Men’)
Like Mr. Lamb, librarians have their problems with borrowers too. People don’t put books back where they should. Instead, returned books are kept at the main desk until a librarian is free to replace them in the right places on the shelves. Even for librarians, putting the right book in the right place can be very time-consuming. But since many libraries are now computerized, you can write a program to help.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically, the librarians will ask your program for a list of books that have been returned so the books can be returned to their correct places on the shelves. Before they are returned to the shelves, the returned books are sorted by author and then title using the ASCII collating sequence. Your program should output the list of returned books in the same order as they should appear on the shelves. For each book, your program should tell the librarian which book (including those previously shelved) is already on the shelf before which the returned book should go.
Input
First, the stock of the library will be listed, one book per line, in no particular order. Initially, they are all on the shelves. No two books have the same title. The format of each line will be:
"title" by author
The end of the stock listing will be marked by a line containing only the word:
END
Following the stock list will be a series of records of books borrowed and returned, and requests from librarians for assistance in restocking the shelves. Each record will appear on a single line, in one of the following formats:
BORROW title
RETURN title
SHELVE
The list will be terminated by a line containing only the word:
END
Output
Each time the SHELVE command appears, your program should output a series of instructions for the librarian, one per line, in the format:
Put title1 after title2 or, for the special case of the book being the first in the collection:
Put title first
After the set of instructions for each SHELVE, output a line containing only the word:
END
Assumptions & Limitations:
1. A title is at most 80 characters long.
2. An author is at most 80 characters long.
3. A title will not contain the double quote (") character.
Sample Input
"The Canterbury Tales" by Chaucer, G.
"The Canterbury Taless" by Chaucer, B.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchiee, D.
"The C Programming Languag" by Kernighan, B. and Ritchiee, D.
"The D Programming Language" by Kernighan, B. and Ritchiee, D.
"A House for Mr. Biswas" by Naipaul, V.S.
"A Congo Diary" by Naipaul, V.S.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
BORROW "The C Programming Languag"
BORROW "The Canterbury Taless"
SHELVE
RETURN "Algorithms"
RETURN "The Canterbury Taless"
SHELVE
RETURN "The C Programming Languag"
SHELVE
BORROW "The C Programming Languag"
BORROW "The Canterbury Taless"
BORROW "A House for Mr. Biswas"
RETURN "The Canterbury Taless"
SHELVE
RETURN "The C Programming Language"
RETURN "A House for Mr. Biswas"
SHELVE
END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END
HINT
使用结构体数组进行操作。定义一个结构体,成员为头作者和书名两个字符串。排序使用sort(),但要自己定义排序方法。另外,借出书籍的时候,要想定位并删除此元素要自己定义一个指针,使用find()函数无法对结构体进行比较,在结构体里面重载运算符应该是可以的,但小白表示不会。。。所以定义一个auto指针来查找。因为输出书名需要双引号所以存入就不去除双引号了。
整体的框架很好理解,录入书籍整行读取,然后分割书名和作者。然后读入指令,对指令区分操作,小细节还是比较多的,具体看代码。
#include<bits/stdc++.h>
using namespace std;
struct books {
	string title;		//书名
	string name;		//作者
};
bool compare(books a, books b) {	//自定义的排序方法,用于sort
	if (a.name == b.name)return a.title < b.title;
	return a.name < b.name;
}
int main(){
	vector<books>list;		//目前书架上的书籍
	vector<books>ret;		//归还的书籍
	map<string, string>alllist;	//存储书籍副本
	string s,title;books temp;
	while (getline(cin, s) && s != "END") {			//录入书籍信息
		int i=s.find('\"',1);			//对书籍进行分割
		temp.title = s.substr(0, i+1);
		temp.name = s.substr(i + 1, s.size() - 1);
		list.push_back(temp);			//存入数组并且保存map副本
		alllist[temp.title] = temp.name;
	}
	while (cin>>s&&s!="END"){
		if (s == "SHELVE") {			//对未上架的图书上架处理
			sort(ret.begin(), ret.end(), compare);	//排序
			for (int i = 0;i < ret.size();i++) {	//挨个书籍处理
				temp = ret[i];
				list.push_back(temp);				//上架
				sort(list.begin(), list.end(), compare);//排序
				for (int i = 0;i < list.size();i++) {	//查找并输出
					if (list[i].name == temp.name && list[i].title == temp.title) {
						if(i==0)cout << "Put " << temp.title << " first" << endl;
						else cout << "Put " << temp.title << " after " << list[i-1].title << endl;
						break;
					}
				}
			}
			ret.clear();		//每次输出完毕必须清空记录,一面影响下一次的使用
			cout << "END" << endl;
		}
		else {
			getchar();			//吃掉空格
			getline(cin, title);//读入标题
			temp.title = title;	//保存到临时结构体
			temp.name = alllist[title];
			if (s == "BORROW") {
				auto pointer = list.begin();//本来使用find()函数不会重载所以自己定义指针查找
				while (!((*pointer).title == temp.title))pointer++;
				list.erase(pointer);		//删除
			}
			else if (s == "RETURN")ret.push_back(temp);//记录还书信息
		}
	}
}
Borrowers UVA - 230的更多相关文章
- uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
		I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ... 
- UVa 230 Borrowers(map和set)
		I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ... 
- Uva - 230 - Borrowers
		AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ... 
- UVA 230 Borrowers (STL 行读入的处理 重载小于号)
		题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ... 
- 【习题 5-8 UVA - 230】Borrowers
		[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ... 
- UVa 1640 (计数) The Counting Problem
		题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ... 
- UVA 10194 (13.08.05)
		:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ... 
- UVa 1640 - The Counting Problem(数论)
		链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ... 
- Fast Matrix Operations(UVA)11992
		UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ... 
随机推荐
- linux系统导出随笔
			导出时,不要用root用户忽略某张表的命令(多张表则直接往后加即可) --ignore-table=firewall_8088.operate_history --ignore-table=firew ... 
- JVM相关 - 深入理解 System.gc()
			本文基于 Java 17-ea,但是相关设计在 Java 11 之后是大致一样的 我们经常在面试中询问 System.gc() 究竟会不会立刻触发 Full GC,网上也有很多人给出了答案,但是这些答 ... 
- windows server 2008 r2 AD域服务器设置
			域控制器是指在"域"模式下,至少有一台服务器负责每一台联入网络的电脑和用户的验证工作,相当于一个单位的门卫一样,称为"域控制器(Domain Controller,简写为 ... 
- 翻译:《实用的 Python 编程》02_07_Objects
			目录 | 上一节 (2.6 列表推导式) | 下一节 (3 从程序组织) 2.7 对象 本节介绍有关 Python 内部对象模型的更多详细信息,并讨论一些与内存管理,拷贝和类型检查有关的问题. 赋值 ... 
- Power BI成功的背后
			Power BI成功的背后 魔力象限 又是一年Gartner数据分析与BI魔力象限报告的发布,Power BI毫无悬念的第一,并且拉开与其他产品的差距越来越大.在Power BI dataflows( ... 
- c++指针数组与二维数组的最大区别
			下面随笔是关于指针数组说明及与二维数组的最大区别. 指针数组 数组的元素是指针型 例 利用指针数组存放矩阵 1 #include 2 using namespace std; 3 int main() ... 
- C#连接Excel读取与写入数据库SQL ( 上 )
			第一次写C#与sql的东西,主要任务是从Excel读取数据,再存到SQL server中. 先上读取Excel文件的code如下. public bool GetFiles(string equipN ... 
- IDEA中便捷内存数据库H2的最简使用方式
			在IDEA中有时候为了练习,需要使用到数据库,但如果自己工作或开发机子上本来没有安装数据库,也没有可用的远程数据库时,我们可以直接在IDEA环境上使用便捷式的内存数据库H2,关于H2更多知识就自己去找 ... 
- SpringBoot源码修炼—系统初始化器
			SpringBoot源码修炼-系统初始化器 传统SSM框架与SpringBoot框架简要对比 SSM搭建流程 缺点: 耗时长 配置文件繁琐 需要找合适版本的jar包 SpringBoot搭建流程 优点 ... 
- 安装VMTools失败的三类解决方法(Windows、Linux、MacOs)
			前言 写这篇笔记的原因,是前几天在虚拟机 Vmware 中重新安装了几个操作系统,突然发现 VMTools 这个工具成了一个特殊的问题,以前还没有发现,因为通常它就给你自动安装了.但是大多数时候也是需 ... 
