**甲级PAT 1022 Digital Library (map映射)

本文介绍了一个高效的图书查询系统设计,该系统能够处理大量图书信息,包括书名、作者、关键字、出版社和出版年份,通过使用map和set数据结构,实现了快速精确的查询功能。

1022 Digital Library (30 分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​4​​) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title -- a string of no more than 80 characters;
  • Line #3: the author -- a string of no more than 80 characters;
  • Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher -- a string of no more than 80 characters;
  • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print Not Found instead.

Sample Input:

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output:

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

题目要求: 

给出N本书,每本书包含7位数的ID,书名,作者名,不超过5个关键字(每个关键字之间用空格隔开),出版社,出版年份这些信息。然后输入M个查询条件。1代表书名,2代表作者名,3代表关键字,4代表出版社,5代表出版年份。输出根据条件查询的所有书的ID号,按从小到大的顺序。若不满足输出Not Found

解题思路:

利用map映射配对除了ID的所有条件,建立一个map<string,set<int> >,把相应的条件插入到搜索的集合里。利用find查找对应条件,如果找到则将set集合的ID全部输出,如果找不到则输出Not Found

注意:

1.读取空格。c++里的cin遇到Tab,空格,回车都会停止。要读取空格需要利用getline(cin,ttitle);可以将一行读取到ttitle字符串中。

2.读取一行的关键字。可以利用循环cin不断输入。每次输入一次关键字之后读取接下来的一个字符char c= getchar()。如果c==‘\n’就说明所有关键字都已经读完,需要跳出循环。

3.读取时要严格按照要求的格式。

之前在输入第一个ID时直接用cin读取,也没有对后面的‘\n’做处理,所以输入数据没有输入完就结束了。这里有两种处理办法,第一种是cin后加一个c = getchar()将‘\n’读取。还有一种利用scanf(“%d\n”,id);

在输入查询条件时也需要按要求读取,可以先scanf("%d: ",&k);将前面的数字读取,然后利用getline(cin,s)读取后面具体输入的条件

4.利用函数查询时需要加引用,否则最后一个测试点会超时。

5.输出ID时由于用int型变量存储,不足7位的要在前面补0。printf("%07d\n",*it);

完整代码:

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

map<string,set<int> >title,author,keywords,publisher,year;

void findid(map<string,set<int> >&m,string &s){
	if(m.find(s)!=m.end()){
		set<int>::iterator it;
		for(it = m[s].begin(); it != m[s].end(); it++) printf("%07d\n",*it);
	}else{
		cout<<"Not Found\n";
	}
}

int main(){
	string ttitle,tauthor,key,tpublisher,tyear,s;
	int N,id,i,M,k;
	cin>>N;
	for(i=1;i<=N;i++){
		scanf("%d\n", &id);
		getline(cin,ttitle);
		title[ttitle].insert(id);
		getline(cin,tauthor); 
		author[tauthor].insert(id);
		while(cin>>key){
			keywords[key].insert(id);
			char c = getchar();
			if(c =='\n') break;
		}
		getline(cin,tpublisher); 
		publisher[tpublisher].insert(id);
		getline(cin,tyear); 
		year[tyear].insert(id);
	}
	cin>>M;
	for(i=1;i<=M;i++){
		scanf("%d: ",&k);
		getline(cin,s);
		cout<<k<<": "<<s<<endl; 
		switch(k){
			case 1: findid(title,s);break;
			case 2: findid(author,s);break;
			case 3: findid(keywords,s);break;
			case 4: findid(publisher,s);break;
			default: findid(year,s);break;
		}
	}
	
	return 0;
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值