【1022 30 模拟】 Digital Library
传送门
题意
给定 \(n\) 本书的信息,每本书包括
- \(id,7\)位整数
- \(title\),书的题目,包含空格
- \(author\),作者,包含空格
- \(keywods\),关键字,以空格分开
- \(publisher\), 包含空格
- \(year\),4位数字
给定 \(m\) 个询问,编号 \(1\sim 5\) 分别对应除了 \(id\) 的 \(5\) 个信息,求出包含的书的 \(id\) ,升序,其中 \(keywords\) 可能存在多个不同书
数据范围
\(1\leq n\leq 10^{4}\)
\(1\leq m\leq 1000\)
题解
- 使用
getline()时,上一个输入必须将换行符号吃掉,使用cin.get()或getchar() - 使用
stringstream可以方便的将含有空格的字符串按照空格分割 - 每个属性都简历一个映射哈希表,哈希表的
value为set, 其能自动排序 s.substr(int l, int r),获取 \([l, r)\) 区间的子串s.substr(x)获取 \([x, end]\) 的子串
Code
#include <bits/stdc++.h>
using namespace std;
vector<unordered_map<string, set<string>>> books(5);
int main() {
int n; cin >> n; cin.get();
for (int i = 0; i < n; i++) {
string id; cin >> id; cin.get();
for (int j = 0; j < 5; j++) {
string s; getline(cin, s);
if (j == 2) {
stringstream ss(s);
string key;
while (ss >> key) {
books[j][key].insert(id);
}
} else {
books[j][s].insert(id);
}
}
}
int m; cin >> m; cin.get();
for (int i = 0; i < m; i++) {
string s; getline(cin, s);
cout << s << endl;
int op = stoi(s.substr(0, s.find(':')));
op--;
s = s.substr(s.find(':') + 2);
if (not books[op].count(s)) {
cout << "Not Found" << endl;
continue;
}
for (auto& it : books[op][s]) {
cout << it << endl;
}
}
}

浙公网安备 33010602011771号