1022 Digital Library
题目大意
输入书籍信息,然后根据顾客查找的信息,返回对应书籍的id,有可能不止一个id
算法思想
- 用map会简单很多,而且map会将关键字按大小排列,刚好符合题目要求
- 输入有些困难,不过以后熟悉了就好,因为要接收带空格的字符串
- 别忘了每次cin后要跟着一个输入多余回车的函数
- 题目较复杂,但是不算难题,仔细分析就能求出
- 没啥特殊测试点,写出来就是满分
代码
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct book//书籍内容
{
string title, author, publisher, year;
vector<string>keywords;
};
struct query//询问内容
{
int wb;
string con;
};
map<string, book>library;
vector<query>cust;
void find_b(int x, string s)//查找字典,输出对应的id
{
bool flag = false;
auto it = library.begin();
if (x == 1)
for (; it != library.end(); it++)
{
if (s == it->second.title)
{
flag = true;
cout << it->first << endl;
}
}
else if (x == 2)
for (; it != library.end(); it++)
{
if (s == it->second.author)
{
flag = true;
cout << it->first << endl;
}
}
else if (x == 3)
for (; it != library.end(); it++)
{
if (find(it->second.keywords.begin(), it->second.keywords.end(), s) != it->second.keywords.end())//vector内查找
{
flag = true;
cout << it->first << endl;
}
}
else if (x == 4)
for (; it != library.end(); it++)
{
if (s == it->second.publisher)
{
flag = true;
cout << it->first << endl;
}
}
else if (x == 5)
for (; it != library.end(); it++)
{
if (s == it->second.year)
{
flag = true;
cout << it->first << endl;
}
}
if (!flag)
cout << "Not Found" << endl;
return;
}
int main()
{
int n=0, m;
cin >> n;
cin.get();//吃掉回车
for (int i = 0; i < n; i++)//输入书籍信息
{
string id,word;
getline(cin, id);
getline(cin, library[id].title);
getline(cin, library[id].author);
while (cin >> word)//输入每个关键字
{
library[id].keywords.push_back(word);
if (cin.get() == '\n') break;
}
getline(cin, library[id].publisher);
cin >> library[id].year;
cin.get();
}
cin >> m;
cin.get();
cust.resize(m);
for (int i = 0; i < m; i++)//输入询问内容
{
int num;
string s;
cin >> num;
getchar();
getchar();
getline(cin, s);
cust[i].wb = num;
cust[i].con = s;
}
for (int i = 0; i < m; i++)//输出内容
{
cout << cust[i].wb << ": " << cust[i].con << endl;
find_b(cust[i].wb,cust[i].con);
}
return 0;
}