1022. Digital Library (30)

1.题目给出一些书本的信息,根据查询的信息,输出书的ID

2.采用map<string, set<string>> 的结构进行存储,可利用string自身带有的排序进行排序。

3.如果采用map<string, set<int>> ,输出的时候则需要考虑ID为0001111的,前面带有0的情况(主要卡在这里)。

时间限制
1000 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
3
0011111
The Testing Book
Yue Chen
test code debug sort keywords

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:
5: 2011
3: blablabla

*/
int str2int(string s)
{
	int a = 0;
	for (int i = 0; i < s.size(); i++)
	{
		a = s[i] - '0' + a * 10;
	}
	return a;
}
int main(void)
{
	string bookNumStr;
	getline(cin, bookNumStr);
	int bookNum;
	bookNum = str2int(bookNumStr);
	//最终查询输出的是ID,所以针对每个查询的词条,都建立一个ID集合
	//使用set存储ID,直接进行了排序
	map<string, set<string>> keyWord;//1000*80B+5*10000*7B=80K+350K=430K
	map<string, set<string>> publisher;//1000*80B+10000*7B=80K+70K=150K
	map<string, set<string>> year;//2000*4B+10000*7B=8K+70K=78K
	map<string, set<string>> author;//10000*80B+10000*7B=800K+70K=870K
	map<string, set<string>> title;//10000*80B+10000*7B=800K+70K=870K

	for (int i = 0; i < bookNum; i++)
	{
		string ID;
		string IDstr, titleTemp, authorTemp, keyWordTemp, publishedTemp, yearTemp;
		getline(cin, IDstr, '\n');

		getline(cin, titleTemp, '\n');
		getline(cin, authorTemp, '\n');
		getline(cin, keyWordTemp, '\n');
		getline(cin, publishedTemp, '\n');
		getline(cin, yearTemp, '\n');

		//ID = str2int(IDstr);
		ID = IDstr;

		int start = 0;
		for (int j = 0; j < keyWordTemp.size(); j++)
		{
			if (keyWordTemp[j] == ' ')
			{
				string temp = keyWordTemp.substr(start, j - start);
				start = j + 1;//跳过空格
				keyWord[temp].insert(ID);
			}
		}
		if (keyWordTemp.substr(start) != "")
			keyWord[keyWordTemp.substr(start)].insert(ID);
		if (titleTemp != "")
			title[titleTemp].insert(ID);
		if (authorTemp != "")
			author[authorTemp].insert(ID);
		if (publishedTemp != "")
			publisher[publishedTemp].insert(ID);
		if (yearTemp != "")
			year[yearTemp].insert(ID);
	}
	string queryStr;
	getline(cin, queryStr, '\n');
	int query = str2int(queryStr);
	for (int i = 0; i < query; i++)
	{
		string s;
		getline(cin, s, '\n');
		string outStr = s;
		int num;
		num = s[0] - '0';
		s = s.substr(3);
		cout << outStr << endl;
		if (num == 1 && title.find(s) != title.end())
		{//查询的是title
			for (set<string>::iterator ite = title[s].begin(); ite != title[s].end(); ite++)
				cout << *ite << endl;
		}
		else if (num == 2 && author.find(s) != author.end())
		{//查询的是作者
			for (set<string>::iterator ite = author[s].begin(); ite != author[s].end(); ite++)
				cout << *ite << endl;
		}
		else if (num == 3 && keyWord.find(s) != keyWord.end())
		{//查询的是关键词
			for (set<string>::iterator ite = keyWord[s].begin(); ite != keyWord[s].end(); ite++)
				cout << *ite << endl;
		}
		else if (num == 4 && publisher.find(s) != publisher.end())
		{//查询的是出版商
			for (set<string>::iterator ite = publisher[s].begin(); ite != publisher[s].end(); ite++)
				cout << *ite << endl;
		}
		else if (num == 5 && year.find(s) != year.end())
		{//查询的是年份
			for (set<string>::iterator ite = year[s].begin(); ite != year[s].end(); ite++)
				cout << *ite << endl;
		}
		else
		{//没有找到对应结果
			cout << "Not Found" << endl;
		}
	}
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注