PAT 1022. Digital Library
来源:互联网 发布:想在淘宝直播怎么弄 编辑:程序博客网 时间:2024/05/05 09:20
【题目链接】
题意:给定一组图书信息,查询与作者,出版社等等图书信息对应的图书,并升序输出这些图书的ID,图书ID必须是七位数字。
思路,设置一个map数组,键为图书信息,用一个string变量来保存,而值为对应的图书ID,用一个set容器来存储。这样形成键值对有利于节省图书的查询时间,否则,单纯地进行暴力搜索可能会超时。
代码如下:
#include <iostream>#include <string>#include <map>#include <set>#include <cstdio>using namespace std;int n, m;map<string, set<int> > info[5];int main(){ //freopen("in_1022.txt", "r", stdin); cin >> n; int ID; for(int i = 0; i < n; i++) { cin >> ID; getchar(); //接收一个回车符,因为getline遇回车符结束 for(int j = 0; j < 5; j++) { string temp; getline(cin, temp); if(j != 2) info[j][temp].insert(ID); else { string str; for(int k = 0; k < temp.size(); k++) { if(temp[k] == ' ') { info[2][str].insert(ID); str.clear(); } else str += temp[k]; if(k == temp.size() - 1) info[2][str].insert(ID); } } } } cin >> m; for(int i = 0; i < m; i++) { int index; string str; cin >> index; getchar();getchar(); getline(cin, str); cout << index << ": " << str << endl; set<int> bookID = info[index - 1][str]; set<int>::iterator it; for(it = bookID.begin(); it!=bookID.end(); it++) printf("%07d\n", *it); if(bookID.empty()) cout << "Not Found" << endl; }}
0 0
- pat 1022. Digital Library
- PAT 1022. Digital Library
- PAT--1022. Digital Library
- PAT 1022. Digital Library
- 【PAT】1022. Digital Library
- PAT 1022. Digital Library (30)
- pat 1022. Digital Library (30)
- PAT 1022. Digital Library (30)
- PAT 1022. Digital Library (30)
- PAT 1022. Digital Library (30)
- 【PAT】1022. Digital Library (30)
- [pat]1022. Digital Library (30)
- [PAT-甲级]1022.Digital Library
- PAT-AL 1022. Digital Library
- 【PAT Advanced Level】1022. Digital Library (30)
- 浙大PAT 1022题 1022. Digital Library
- PAT A 1022. Digital Library (30)
- PAT习题解:1022. Digital Library (30)
- 四 导入
- 加密服务
- ORACLE 11G在同一台linux服务器从实例1全库导入到实例2上
- 面试题:是否可以从一个static方法内部发出对非static方法的调用?
- OJ刷题---希尔排序
- PAT 1022. Digital Library
- Android 通过名称获取资源ID
- 进程切换、创建、加载
- Android基础系列-----------Android系统启动过程简析
- JavaScript DOM 编程艺术 第二版 学习笔记。
- C++ 智能指针详解
- (转载)ADS启动问题:The unexpected failure of a COM operation suggests that MWComHelpers may be missing
- Objective-C初接触 课堂笔记
- 面试题:Math.round(11.5)等於多少? Math.round(-11.5)等於多少?