C++ 代码
#include <iostream>
#include <unordered_map>
#include <set>
using namespace std;
unordered_map<string, set<string>> title, author, key, pub, year;
void query(unordered_map<string, set<string>> &map, string s)
{
if (map.find(s) != map.end())
for (auto res : map[s])
cout << res << endl;
else
cout << "Not Found" << endl;
}
int main()
{
int n;
cin >> n;
while (n--)
{
string id, _title, _author, _key, _pub, _year;
cin >> id;
getchar();
getline(cin, _title), title[_title].insert(id);
getline(cin, _author), author[_author].insert(id);
while (cin >> _key)
{
key[_key].insert(id);
if (getchar() == '\n')
break;
}
getline(cin, _pub), pub[_pub].insert(id);
cin >> _year, year[_year].insert(id);
}
int m;
cin >> m;
while (m--)
{
int x;
scanf("%d: ", &x);
string str;
getline(cin, str);
cout << x << ": " << str << endl;
switch (x)
{
case 1: query(title, str); break;
case 2: query(author, str); break;
case 3: query(key, str); break;
case 4: query(pub, str); break;
case 5: query(year, str); break;
}
}
return 0;
}
倒排索引 太妙了
##### 我也是用unordered_map