pat 1022

题意:每本书有id,书名,作者,keyword,出版社和出版年份,读入这些信息,用任意一个信息查询到所有相关的书

思路:map+字符串什么的各种存一下就可以了,注意id必须是7位,会有前导0,所以用int保存的话注意用%07d输出

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
using namespace std;

map<string,int > Map[5];
vector<int > list[5][10005];
int n,m;
int ll[5];
char str[10005],name[10005];

int main()
{
	int i,j,a;
	scanf("%d",&n);
	for(i=0;i<5;i++)
		Map[i].clear();
	memset(ll,0,sizeof(ll));
	for(i=0;i<n;i++)
	{
		int id;
		scanf("%d",&id);
		getchar();
		//title
		gets(str);
		if(Map[0].find(str)==Map[0].end())
			Map[0][str]=ll[0]++;
		list[0][Map[0][str]].push_back(id);

		//author
		gets(str);
		if(Map[1].find(str)==Map[1].end())
			Map[1][str]=ll[1]++;
		list[1][Map[1][str]].push_back(id);

		//keyword
		gets(str);
		int temp=0;
		for(j=0;str[j]!='\0';j++)
		{
			if(str[j]==' '&&temp!=0)
			{
				name[temp]='\0';
				if(Map[2].find(name)==Map[2].end())
					Map[2][name]=ll[2]++;
				list[2][Map[2][name]].push_back(id);
				temp=0;
			}
			else name[temp++]=str[j];
		}
		if(temp!=0)
		{
			name[temp]='\0';
			if(Map[2].find(name)==Map[2].end())
				Map[2][name]=ll[2]++;
			list[2][Map[2][name]].push_back(id);
		}
		//publisher
		gets(str);
		if(Map[3].find(str)==Map[3].end())
			Map[3][str]=ll[3]++;
		list[3][Map[3][str]].push_back(id);

		//year
		gets(str);
		if(Map[4].find(str)==Map[4].end())
			Map[4][str]=ll[4]++;
		list[4][Map[4][str]].push_back(id);
	}
	scanf("%d",&m);
	getchar();
	for(i=0;i<m;i++)
	{
		gets(str);
		strcpy(name,str+3);
		int id=str[0]-'0'-1;
		puts(str);
		if(Map[id].find(name)==Map[id].end())
			puts("Not Found");
		else 
		{
			sort(list[id][Map[id][name]].begin(),list[id][Map[id][name]].end());
			for(j=0;j<list[id][Map[id][name]].size();j++)
			{
				if(j!=0&&list[id][Map[id][name]][j]==list[id][Map[id][name]][j-1])
					continue;
				printf("%07d\n",list[id][Map[id][name]][j]);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值