1022. Digital Library (30)

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

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


//#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);
	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())
		{
			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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值