AcWing 1499. 数字图书馆
原题链接
中等
作者:
leo123456
,
2020-08-20 17:10:26
,
所有人可见
,
阅读 441
#include<iostream>
#include <algorithm>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
struct Book {
string id, name, author;
set<string> keywords; //很多关键字,用set
string publisher, year;
};
main()
{
int n, m;
cin >> n;
vector<Book> books;
while (n--)
{
string id, name, author;
cin >> id;
getchar(); //使用getline前把前面cin的换行符吸掉
getline(cin, name);
getline(cin, author);
string line;
getline(cin, line);
stringstream ssin(line);//字符流,流进去
//ssin<<line;
string keyword;
set<string> keywords;
while (ssin >> keyword) keywords.insert(keyword); //顺序一个个流出
string publisher, year;
getline(cin, publisher);
cin >> year;
books.push_back({ id,name,author,keywords,publisher,year });
}
cin >> m;
getchar();
string line;
while (m--)
{
getline(cin, line);
cout << line << endl;
string info = line.substr(3); //取子串
char t = line[0];
vector<string> res;
if (t == '1')
{
for (auto& book : books)
if (book.name == info)
res.push_back(book.id);
}
else if (t == '2')
{
for (auto& book : books) //加&不然运行超时一个点 ,不用复制,直接引用
if (book.author == info)
res.push_back(book.id);
}
else if (t == '3')
{
for (auto& book : books)
if (book.keywords.count(info))
res.push_back(book.id);
}
else if (t == '4')
{
for (auto& book : books)
if (book.publisher == info)
res.push_back(book.id);
}
else if (t == '5')
{
for (auto& book : books)
if (book.year == info)
res.push_back(book.id);
}
if (res.empty()) cout << "Not Found" << endl;
else
{
sort(res.begin(), res.end());
for (auto id : res) cout << id << endl;
}
}
return 0;
}