当前位置: 首页 > 工具软件 > yue-library > 使用案例 >

PAT-A1022.Digital-Library

单于旭东
2023-12-01

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 (≤10000) 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

题目大意:一本书包含6个信息,分别为:ID(唯一),书名,作者,关键字,出版社,出版年份。然后给定一系列查询码:(key:一串字符)。要求你查询此书是否存在,存在按升序输出ID,不存在输出Not Found。

解题思路:此题比较简单,思路也比较简单,不多赘述。唯一需要注意的是关键字查询时,每本书有多个关键字,只需找到一个对应的即可。

代码封装到一个类里
只需在main函数创建此类的一个对象即可,不需要做其它任何操作
代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
//书的结构体
typedef struct Digital_book
{
	string ID;
	string title;
	string author;
	vector<string> keywords;//将每一个关键字存储成一块,方便查询
	string publisher;
	string year;
	bool operator<(const Digital_book& db)//重载<运算符,便于用sort排序
	{
		return this->ID < db.ID;
	}
}Digital_book;
class DigitalLibrary
{
private:
	int N;
	vector<Digital_book> book;
	int M;
	vector<string> query;

public:	
	DigitalLibrary()
	{
		input();//读取数据
		sort(book.begin(), book.end());//重载了<运算符
		traver();遍历用户查询信息
	}
	
	void input()
	{
		scanf("%d", &N);
		char c;
		for (int i = 0; i < N; ++i)
		{
			Digital_book db;
			//cin >> db.ID;
			db.ID.resize(7);
			scanf("%s", &db.ID[0]);
			c = getchar();
			while ((c = getchar()) != '\n')
				db.title.push_back(c);
			while ((c = getchar()) != '\n')
				db.author.push_back(c);
			do
			{
				string word;
				cin >> word;
				db.keywords.push_back(word);
			} while ((c = getchar()) != '\n');
			while ((c = getchar()) != '\n')
				db.publisher.push_back(c);
			//cin >> db.year;
			db.year.resize(4);
			scanf("%s", &db.year[0]);
			book.push_back(db);
		}
		scanf("%d", &M);
		query.resize(M);
		c = getchar();
		for (int i = 0; i < M; ++i)
		{
			while ((c = getchar()) != '\n')
			{
				query[i].push_back(c);
			}
		}
	}

	void traver()
	{
		for (vector<string>::iterator it = query.begin(); it != query.end(); ++it)
		{
			printf("%s\n", (*it).c_str());
			char key = (*it)[0];//用户查询码前的数字1,2,3,4,5.对应于不同的查找类别
			string str((*it).begin() + 3, (*it).end());//查询信息
			find_book(key, str);查询接口
		}
	}

	void find_book(const char& key, const string& str)
	{
		vector<string> result;
		switch (key)//根据key的不同调用不同的查询函数
		{
		case '1': find_title(str, result);
			break;
		case '2': find_author(str, result);
			break;
		case '3': find_keyword(str, result);
			break;
		case '4': find_publisher(str, result);
			break;
		case '5': find_year(str, result);
			break;
		}
		if (result.empty()) printf("Not Found\n");
		for (vector<string>::iterator it = result.begin(); it != result.end(); ++it)
			printf("%s\n", (*it).c_str());
	}
//下面是不同的查询函数
	void find_title(const string& str, vector<string>& res)
	{
		for (vector<Digital_book>::iterator it = book.begin(); it != book.end(); ++it)
		{
			if (str == (*it).title)
				res.push_back((*it).ID);
		}
	}

	void find_author(const string& str, vector<string>& res)
	{
		for (vector<Digital_book>::iterator it = book.begin(); it != book.end(); ++it)
		{
			if (str == (*it).author)
				res.push_back((*it).ID);
		}
	}

	void find_keyword(const string& str, vector<string>& res)
	{
		for (vector<Digital_book>::iterator it = book.begin(); it != book.end(); ++it)
		{
			for (vector<string>::iterator vs_it = (*it).keywords.begin();
				vs_it != (*it).keywords.end(); ++vs_it)
			{
				if (str == (*vs_it))
				{
					res.push_back((*it).ID);
					break;
				}
			}
		}
	}

	void find_publisher(const string& str, vector<string>& res)
	{
		for (vector<Digital_book>::iterator it = book.begin(); it != book.end(); ++it)
		{
			if (str == (*it).publisher)
				res.push_back((*it).ID);
		}
	}

	void find_year(const string& str, vector<string>& res)
	{
		for (vector<Digital_book>::iterator it = book.begin(); it != book.end(); ++it)
		{
			if (str == (*it).year)
				res.push_back((*it).ID);
		}
	}
};

//这个代码结构和逻辑应该比较清晰,欢迎大家讨论。

 类似资料: