PAT

PAT1022 Digital Library

当错题成为一种常态

Posted by canjuly on September 6, 2018

题目链接

题目大意

description

给出n本书,每本书有6个信息,分别是图书id,书名,作者,关键词,出版社,出版年份。其中关键词会有不止一个,关键词不含空格。接下来有m次询问,给出5种询问方式,1代表按书名询问,2代表按作者询问,3代表按关键词询问,4代表按出版社询问,5代表出版年份询问。若找到对应书籍则按升序输出图书id,否则输出Not Found。

思路

inspration

没啥算法,就是模拟。拿一堆map记录对应关系,然后拿一堆set数组记录作者、书名等等对应的图书id就好了。

坑点

pit

我的代码clang可以运行,g++运行会错误,八成是回车的锅。。写的太难看了。总之敲好交一发有一个case显示段错误,这时一个不祥的预感突然涌上心头,我颤抖的把数组改大了一点,然后。。。就过了。。淦啊!说好的年份小于3000的呢!经过我一次又一次的测试,发现年份有个3020的case,无语了,这题目都啥玩意。。

代码

括号换行,又臭又长

#include <iostream>  
#include <cstdio>  
#include <cmath>  
#include <cstdlib>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#define ll long long  
using namespace std;  

const int INF = 0x3f3f3f3f;
set<string>name[10002],author[10002],key[1002],publish[1002],year[4002];
set<string>::iterator it;
map<string,int>namemap,authormap,keymap,publishmap;
int main()
{
	int n,count[10];
	for(int i=1;i<=10;i++)
		count[i]=0;
	scanf("%d",&n);
	string str,id;
	for(int i=1;i<=n;i++)
	{
		cin>>id;getchar();
		getline(cin,str);
		if(namemap.count(str)==0)
		{
			count[1]++;
			namemap[str]=count[1];
		}
		name[namemap[str]].insert(id);
		getline(cin,str);
		if(authormap.count(str)==0)
		{
			count[2]++;
			authormap[str]=count[2];
		}
		author[authormap[str]].insert(id);
		getline(cin,str);
		string temp="";int len = str.size();
		for(int j=0;j<len;j++)
		{
			if(str[j]==' ')
			{
				if(keymap.count(temp)==0)
				{
					count[3]++;
					keymap[temp]=count[3];
				}
				key[keymap[temp]].insert(id);
				temp="";
			}
			else 
				temp+=str[j];
		}
		if(temp!="")
		{
			if(keymap.count(temp)==0)
			{
				count[3]++;
				keymap[temp]=count[3];
			}
			key[keymap[temp]].insert(id);
		}
		getline(cin,str);
		if(publishmap.count(str)==0)
		{
			count[4]++;
			publishmap[str]=count[4];
		}
		publish[publishmap[str]].insert(id);
		int y;
		scanf("%d",&y);
		year[y].insert(id);
	}
	int m,now;
	scanf("%d",&m);getchar();
	while(m--)
	{
		int out = 0;
		getline(cin,str);
		cout<<str<<endl;
		char flag=str[0];
		str = str.substr(3,str.length()-3);
		if(flag=='1')
		{
			now = namemap[str];
			for(it=name[now].begin();it!=name[now].end();it++)
			{
				out=1;
				cout<<*it<<endl;
			}
		}
		if(flag=='2')
		{
			now = authormap[str];
			for(it=author[now].begin();it!=author[now].end();it++)
			{
				out=1;
				cout<<*it<<endl;
			}
		}
		if(flag=='3')
		{
			now = keymap[str];
			for(it=key[now].begin();it!=key[now].end();it++)
			{
				out=1;
				cout<<*it<<endl;
			}
		}
		if(flag=='4')
		{
			now = publishmap[str];
			for(it=publish[now].begin();it!=publish[now].end();it++)
			{
				out=1;
				cout<<*it<<endl;
			}
		}
		if(flag=='5')
		{
			now=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');
//			cout<<now<<endl;
			for(it=year[now].begin();it!=year[now].end();it++)
			{
				out=1;
				cout<<*it<<endl;
			}
		}
		if(!out)
			cout<<"Not Found"<<endl;
	}
	return 0;
}