【题目大意】

给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字;然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号。

【思路】

模拟。

【坑】

1) 根据空格判断关键字key,遇到空格存下前一个key,则到最后一个key没有保存,退出while循环后要再存一次。

2) 漏根据id排序

3) Not Found 拼写错了...

4) 题目说年份在[1000, 3000],但是并不是这样的!输出年份一定要保留4位高位填充0补齐,否则测试点1过不了。这个地方我调试了好久,气死人了!

【tips】

1) 输入带空格的string

方法一:

  string s;

  getline(cin, s);

方法二:

  string s;

  char c;

  while((c=cin.get())!='\n')

    s = s + c;

2) 题目中根据id排序,我的处理方法是用集合set存储符合查询条件的id,循环结束后一次输出set里的id值即可,因为set会自动帮你排序。(时间3ms;4ms;4ms;4ms;280ms)

也可以先调用sort排序,后续依次比对输出的时候自然就是按顺序的了,这样会慢一些。(时间5ms;5ms;5ms;5ms;393ms)

3) 网上有人说用map方便,或许今后可以试试?

【AC代码】

 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include <algorithm>
using namespace std;
#define N 10002
struct book {
string id;
string title, author;
vector<string> key;
string publisher;
int year;
};
vector<book> info;
int main()
{
int n;
cin >> n;
int i;
for (i = ; i < n; i++)
{
book tbook;
cin >> tbook.id;
char c;
c = cin.get();
getline(cin,tbook.title);
getline(cin, tbook.author);
string str; while (cin >> str) {
tbook.key.push_back(str);
if (getchar() == '\n')
break;
}
/*
while ((c = cin.get()) != '\n')
{
if (c == ' '){
tbook.key.push_back(str);
str.clear();
}
else{
str += c;
}
}
tbook.key.push_back(str);
*/
getline(cin, tbook.publisher);
cin >> tbook.year;
info.push_back(tbook);
}
int m, op;
cin >> m;
for (i = ; i < m; i++)
{
scanf("%d: ", &op);
if (op == ) {
set<string>ans_id;
string ttitle;
getline(cin, ttitle);
cout << "1: " << ttitle << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (ttitle == info[j].title)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tauthor;
getline(cin, tauthor);
cout << "2: " << tauthor << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tauthor == info[j].author)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tkey;
getline(cin, tkey);
cout << "3: " << tkey << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
for (int k = ; k < info[j].key.size(); k++)
{
if (tkey == info[j].key[k])
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
break;
}
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tpub;
getline(cin, tpub);
cout << "4: " << tpub << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tpub == info[j].publisher)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
int tyear;
cin >> tyear;
//cout << "5: " << tyear << endl;
printf("5: %04d\n", tyear);
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tyear == info[j].year)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
}
/*for (i = 0; i < info.size(); i++)
{
cout << info[i].id << endl;
cout << info[i].title << endl;
cout << info[i].author << endl;
for (int j = 0; j < info[i].key.size(); j++)
cout << info[i].key[j] << endl;
cout << endl;
cout << info[i].publisher << endl;
cout << info[i].year << endl;
}*/
return ;
}

最新文章

  1. C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
  2. BZOJ3522——[Poi2014]Hotel
  3. 改变ubuntu终端显示语言(桌面系统是中文,终端提示是英文)
  4. jQuery的目标
  5. 怎么让alert弹出框的内容可以换行?
  6. 【英语】Bingo口语笔记(48) - 关于春节的表达
  7. angular过滤器基本用法
  8. Jmeter之性能压测Stepping Thread Group 逐步增加并发数 阶梯式加压并发 (十五)
  9. data数据不一致的问题
  10. 基础select语句详解
  11. 2018 大湾区(深圳) .NET技术分享交流会 第一期
  12. 初识区块链——用JS构建你自己的区块链
  13. 线程的start方法和run方法的区别
  14. dubbo入门学习 三 dubbo简介
  15. 原子性 CAS算法
  16. linux中fork, source和exec的区别
  17. jq closet的使用,找到距离最近的一个父元素;
  18. 【转】Principles of training multi-layer neural network using backpropagation
  19. 学习Kali Linux必须知道的几点
  20. 二分查找算法(Python版)

热门文章

  1. Go语言实现:【剑指offer】链表中倒数第k个结点
  2. My introduction
  3. 《Head first设计模式》之外观模式
  4. vue学习(二)模板页配置(bootstrap)
  5. 使用GitHub+Travis-CI+Docker打造自动化流水线
  6. 基于 H5 Canvas 实现楼宇新风系统
  7. 解决关闭SSH进程CobaltStrike自动关闭
  8. 使用Vue.prototype在vue中注册和使用全局变量
  9. 【学习笔记】:JavaScript中的BOM对象
  10. JavaScript之BOM基础