[PAT解题报告] Digital Library
不是难题。其实就是按照name, title, publisher, key
word和year建立倒排索引就行了。具体倒排索引可以用map<string, set<string> >
代替。key就是那5种类型,value是符合要求的书的集合。题目一点也不难,可以算做模拟题,就是问什么,从倒排索引里把要的东西拿出来输出就可以了。
注意事项: 别忘了输出query……
代码:
#include <cstdio>
#include <map>
#include <string>
#include <cstring>
#include <sstream>
#include <set>
using namespace std;
map<string, set<string> > title, author,word, publisher, year;
char s[1000];
void make(map<string,set<string> > &a, const char *s) {
map<string,set<string> >::iterator t = a.find(s);
if ((t == a.end()) || (t->second.empty())) {
puts("Not Found");
}
else {
for (set<string>::iterator it = t->second.begin(); it != t->second.end(); ++it) {
puts(it->c_str());
}
}
}
int main() {
int n;
scanf("%d",&n);
for (gets(s);n;--n) {
gets(s);
string id = s;
gets(s);
title[s].insert(id);
gets(s);
author[s].insert(id);
gets(s);
istringstream in(s);
string temp;
while (in >> temp) {
word[temp].insert(id);
}
gets(s);
publisher[s].insert(id);
gets(s);
year[s].insert(id);
}
scanf("%d",&n);
for (gets(s);n;--n) {
gets(s);
puts(s);
switch(s[0]) {
case '1':
make(title, s + 3);
break;
case '2':
make(author, s + 3);
break;
case '3':
make(word, s + 3);
break;
case '4':
make(publisher, s + 3);
break;
case '5':
make(year, s + 3);
break;
}
}
return 0;
}
原题链接: http://www.patest.cn/contests/pat-a-practise/1022
查看10道真题和解析
