Day47: [PAT甲级] 1022 Digital Library (30分)
题源:
来自PAT真题库:
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
代码:
dirty code凑合看吧
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#include<unordered_map>
#include<queue>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
int N,K;
typedef struct book {
int id;
string title;
string author;
set<string> kws;
string publisher;
string year;
}book;
vector<book> books;
unordered_map<string,set<int> >title_map;
unordered_map<string, set<int> >author_map;
unordered_map<string, set<int> >kws_map;
unordered_map<string, set<int> >publisher_map;
unordered_map<string, set<int> >year_map;
int main() {
cin >> N;
books.resize(N);
for (int i = 0; i < N; i++) {
cin >> books[i].id; getchar();
getline(cin, books[i].title);
if (title_map.find(books[i].title) == title_map.end()) {
set<int> se; se.insert(books[i].id);
title_map.insert(make_pair(books[i].title,se));
}
else title_map[books[i].title].insert(books[i].id);
getline(cin, books[i].author);
if (author_map.find(books[i].author) == author_map.end()) {
set<int> se; se.insert(books[i].id);
author_map.insert(make_pair(books[i].author, se));
}
else author_map[books[i].author].insert(books[i].id);
string keywords;
getline(cin, keywords);
keywords.append(1,' ');
while (1) {
int pos = keywords.find(' ');
string keyword=keywords.substr(0, pos);
if (kws_map.find(keyword) == kws_map.end()) {
set<int> se; se.insert(books[i].id);
kws_map.insert(make_pair(keyword, se));
}
else kws_map[keyword].insert(books[i].id);
if (pos != keywords.size() - 1) {
keywords = keywords.substr(pos + 1);
}
else {
if (kws_map.find(keyword) == kws_map.end()) {
set<int> se; se.insert(books[i].id);
kws_map.insert(make_pair(keyword, se));
}
else kws_map[keyword].insert(books[i].id);
break;
}
}
getline(cin, books[i].publisher);
if (publisher_map.find(books[i].publisher) == publisher_map.end()) {
set<int> se; se.insert(books[i].id);
publisher_map.insert(make_pair(books[i].publisher, se));
}
else publisher_map[books[i].publisher].insert(books[i].id);
cin >> books[i].year;
if (year_map.find(books[i].year) == year_map.end()) {
set<int> se; se.insert(books[i].id);
year_map.insert(make_pair(books[i].year, se));
}
else year_map[books[i].year].insert(books[i].id);
}
cin >> K; int x;
for (int i = 0; i < K; i++) {
scanf("%d: ",&x);
if (x == 1) {//title
string title;
getline(cin, title);
if (title_map.find(title) == title_map.end()) {
cout << "1: " << title << endl; cout << "Not Found" << endl; continue;
}
set<int> se=title_map[title];
cout << "1: "<< title<<endl;
for (auto iter = se.begin(); iter != se.end(); iter++) {
printf("%07d\n", *iter);
}
}
else if (x==2) {
string author;
getline(cin, author);
if (author_map.find(author) == author_map.end()) {
cout << "2: " << author << endl; ; cout << "Not Found" << endl; continue;
}
set<int> se = author_map[author];
cout << "2: " << author << endl;
for (auto iter = se.begin(); iter != se.end(); iter++) {
printf("%07d\n", *iter);
}
}
else if (x == 3) {
string keyword;
getline(cin, keyword);
if (kws_map.find(keyword) == kws_map.end()) {
cout << "3: " << keyword << endl; ; cout << "Not Found" << endl; continue;
}
set<int> se = kws_map[keyword];
cout << "3: " << keyword << endl;
for (auto iter = se.begin(); iter != se.end(); iter++) {
printf("%07d\n", *iter);
}
}
else if (x == 4) {
string publisher;
getline(cin, publisher);
if (publisher_map.find(publisher) == publisher_map.end()) {
cout << "4: " << publisher << endl; ; cout << "Not Found" << endl; continue;
}
set<int> se = publisher_map[publisher];
cout << "4: " << publisher << endl;
for (auto iter = se.begin(); iter != se.end(); iter++) {
printf("%07d\n", *iter);
}
}
else if (x == 5) {
string year;
cin >> year;
if (year_map.find(year) == year_map.end()) {
cout << "5: " << year << endl; ; cout << "Not Found" << endl; continue;
}
set<int> se = year_map[year];
cout << "5: " << year << endl;
for (auto iter = se.begin(); iter != se.end(); iter++) {
printf("%07d\n",*iter);
}
}
}
system("pause");
return 0;
}