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

PAT甲级 -- 1022 Digital Library (30 分)

陆弘新
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 (≤10​4​​) which is the total number of books. Then Nblocks 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

没写完,又臭又长!key word处理方法没写出来,参考柳神代码:

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;


int n, m;
struct book
{
	int id;
	char title[80];
	char author[80];
	char key[60];
	char publisher[80];
	int pub_year;
}books[10010];

bool cmp(int a, int b)
{
	return a < b;
}

vector<int> title_q; 
vector<int> author_q;
vector<int> key_q;
vector<int> publisher_q;
vector<int> pub_year_q;

void look_for_titile(char t[])
{
	for(int i = 0; i < n; i++)
	{
		if(strcmp(t, books[i].title) == 0)
		{
			title_q.push_back(books[i].id);
		}		
	}
	sort(title_q.begin(),title_q.end(),cmp);
	if (title_q.size() == 0)
	{
		printf("Not Found\n");
	}else
	{
		for (int i = 0; i < title_q.size(); i++)
		{
			printf("%d\n",title_q[i]);
		}
	}
	
}

void look_for_author(char t[])
{
	for(int i = 0; i < n; i++)
	{
		if(strcmp(t, books[i].author) == 0)
		{
			author_q.push_back(books[i].id);
		}
	}
	sort(author_q.begin(),author_q.end(),cmp);
	if (author_q.size() == 0)
	{
		printf("Not Found\n");
	}else
	{
		for(int i = 0; i < author_q.size(); i++)
		{
			printf("%d\n",author_q[i]);
		}
	}
	
}

void look_for_key(char t[])
{
	char keys[5], ind = 0;
	for(int i = 0; i < strlen(t); i++)
	{
		string s = "";
		if(t[i] != ' ')
		{
			s += t[i];
		}else
		{
			keys[ind] = s;
			s = "";
		}

	}
}

void look_for_publisher(char t[])
{
	for(int i = 0; i < n; i++)
	{
		if(strcmp(t, books[i].publisher) == 0)
		{
			publisher_q.push_back(books[i].id);
		}
		
	}
	sort(publisher_q.begin(),publisher_q.end(),cmp);
	if (publisher_q.size() == 0)
	{
		printf("Not Found\n");
	}else
	{
		for (int i = 0; i < publisher_q.size(); i++)
		{
			printf("%d\n",publisher_q[i]);
		}
	}
	
}

void look_for_pub_year(int t)
{
	for(int i = 0; i < n; i++)
	{
		if(t == books[i].pub_year)
		{
			pub_year_q.push_back(books[i].id);
		}
	}
	sort(pub_year_q.begin(),pub_year_q.end(),cmp);
	if (pub_year_q.size() == 0)
	{
		printf("Not Found\n");
	}else
	{
		for (int i = 0; i < pub_year_q.size(); i++)
		{
			printf("%d\n",pub_year_q[i]);
		}
	}
	
}


int main()
{

	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &books[i].id);
		getchar();
		cin.getline(books[i].title,80);
		cin.getline(books[i].author,80);
		cin.getline(books[i].key,60);
		cin.getline(books[i].publisher,80);
		scanf("%d", &books[i].pub_year);
	}
	scanf("%d", &m);
	getchar();
	while(m--)
	{
		char query[80];
		cin.getline(query,80);
		printf("%s\n", query);
		char num = query[0];
		char* q = query+3;
		switch(num)
		{
			case '1': 
			{
				
				look_for_titile(q);
				break;
			}
			case '2': 
			{
				look_for_author(q);
				break;
			}
			case '3': 
			{
				look_for_key(q);
				break;
			}
			case '4': 
			{
				look_for_publisher(q);
				break;
			}
			case '5': 
			{
				look_for_pub_year(atoi(q));
				break;
			}
		}	
	}
return 0;
}

 柳神的代码:

收获:看大神的代码真的太太太震撼了!!

1.map映射,把一个string映射成一个set,还自动去重!太棒了叭!

2.还有因为不确定key word,采用while(cin >> s)并且判断c = getchar(),c是否等于\n退出循环的方式也叫绝...

3.getline()前面有换行符要用getchar()接收,或者 采用scanf("%d\n",&id)避免掉getchar();

何时我才能有这样的思维!!

#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string, set<int> > title, author, key, pub, year;
void query(map<string, set<int> > &m, string &str) {
	if(m.find(str) != m.end()) {
		for(auto it = m[str].begin(); it != m[str].end(); it++)
			printf("%07d\n", *it);
	} else
		cout << "Not Found\n";
}
int main() {
	int n, m, id, num;
	scanf("%d", &n);
	string ttitle, tauthor, tkey, tpub, tyear;
	for(int i = 0; i < n; i++) {
		scanf("%d\n", &id);
		getline(cin, ttitle);
		title[ttitle].insert(id);
		getline(cin, tauthor);
		author[tauthor].insert(id);
		while(cin >> tkey) {
			key[tkey].insert(id);
			char c = getchar();
			if(c == '\n') break;
		}
		getline(cin, tpub);
		pub[tpub].insert(id);
		getline(cin, tyear);
		year[tyear].insert(id);
	}
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf("%d: ", &num);
		string temp;
		getline(cin, temp);
		cout << num << ": " << temp << "\n";
		if(num == 1) query(title, temp);
		else if(num == 2) query(author, temp);
		else if(num == 3) query(key, temp);
		else if(num == 4) query(pub,temp);
		else if(num ==5) query(year, temp);
	}
	return 0;
}

 

 类似资料: