PAT习题解:1022. Digital Library (30)

题目链接:http://www.patest.cn/contests/pat-a-practise/1022

这道题目我没想到什么好办法做,最后参考了闲云阁的文章,用map 和 set 做出来,算是顺便学习了一下 STL 里面 map 和 set 的用法。话说C++的输入输出真是麻烦啊,这题用Python做的话写起来会很简单吧。

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

map> bookname;
map> author;
map> keywords;
map> publisher;
map> year;

void printSearchResults(map> &m, string query)
{
	if (m[query].size() > 0)
	{
		for (set::iterator it = m[query].begin(); it != m[query].end(); it++)
		{
			cout << *it << endl;
		}
	}
	else
	{
		cout << "Not Found" << endl;
	}
}

int main()
{
	freopen("d:\\in.txt", "r", stdin);

	char c;
	string sid;
	string s;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> sid;
		c = getchar();
		getline(cin, s);
		bookname[s].insert(sid);

		getline(cin, s);
		author[s].insert(sid);

		char str[105];
		cin.getline(str, 105);
		char *p = strtok(str," ");
		while (p)
		{
			s = p;
			keywords[s].insert(sid);
			p = strtok(NULL, " ");
		}

		getline(cin, s);
		publisher[s].insert(sid);

		getline(cin, s);
		year[s].insert(sid);
	}

	cin >> n;
	int item;

	while (n--)
	{
		scanf("%d: ", &item);
		getline(cin, s);
		cout << item << ": " << s << endl;
		switch (item)
		{
			case 1:
				printSearchResults(bookname, s);
				break;
			case 2:
				printSearchResults(author, s);
				break;
			case 3:
				printSearchResults(keywords, s);
				break;
			case 4:
				printSearchResults(publisher, s);
				break;
			case 5:
				printSearchResults(year, s);
				break;
		}
	}
	return 0;
}


你可能感兴趣的:(数据结构与算法)