注意:允许转载,但转载请注明作者和出处
1499. 数字图书馆
题目
答案
#include <iostream>
#include <vector>
#include <cstring>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m;
struct Book
{
string id, name, author;
set<string> keywords;
string publisher;
int year;
}books[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
{
string id, name, author;
cin >> id;
getchar();
getline(cin, name), getline(cin, author);
string line;
string keyword;
set<string> keywords;
getline(cin, line);
stringstream ssin(line);
while(ssin >> keyword) keywords.insert(keyword);
string publisher;
getline(cin, publisher);
int year;
cin >> year;
books[i] = {id, name, author, keywords, publisher, year};
}
// 经下方code检验,上述操作无误
// for (int i = 0; i < n; i ++)
// {
// Book book = books[i];
// cout << book.id << ' ' << book.name << ' ' << book.author << endl;
// for (auto k : book.keywords) cout << k << ' ';
// puts("");
// cout << book.publisher << ' ' << book.year << endl;
// }
cin >> m;
while (m --)
{
// scanf输入字符串的时候,遇见空格就不读了
// scanf内容参考链接:
// https://blog.youkuaiyun.com/m0_46450606/article/details/137961671
int num;
char str[100];
scanf("%d: %[^\n]", &num, str);
vector<string> res;
// cout << num << ' ';
// for (int i = 0; i < 100; i ++) cout << str[i];
// 找到书名在结构体数组中出现的时对应的id
if (num == 1)
{
for (int i = 0; i < n; i ++)
if (books[i].name == str)
res.push_back(books[i].id);
}
else if (num == 2)
{
for (int i = 0; i < n; i ++)
if (books[i].author == str)
res.push_back(books[i].id);
}
else if (num == 3)
{
for (int i = 0; i < n; i ++)
if (books[i].keywords.count(str))
res.push_back(books[i].id);
}
else if (num == 4)
{
for (int i = 0; i < n; i ++)
if (books[i].publisher == str)
res.push_back(books[i].id);
}
else
{
int pub_year = stoi(str);
for (int i = 0; i < n; i ++)
if (books[i].year == pub_year)
res.push_back(books[i].id);
}
printf("%d: %s\n", num, str);
if (res.empty()) puts("Not Found");
else
{
sort(res.begin(), res.end());
for (auto r : res) cout << r << endl;
}
}
return 0;
}