1022 Digital Library (30)(30 point(s))
problem
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:
Line #1: the 7-digit ID number;
Line #2: the book title -- a string of no more than 80 characters;
Line #3: the author -- a string of no more than 80 characters;
Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
Line #5: the publisher -- a string of no more than 80 characters;
Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.
After the book information, there is a line containing a positive integer M (<=1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:
1: a book title
2: name of an author
3: a key word
4: name of a publisher
5: a 4-digit number representing the year
Output Specification:
For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print "Not Found" instead.
Sample Input:
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
Sample Output:
1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found
tip
- 考察map的使用。
answer
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
int N, M;
map<string, set<int> > titleM, authorM, keyM, pubM, yearM;
vector<string> Explode(string s){
string buff = "";
vector<string> v;
for(int i = 0; i < s.size(); i++){
if(s[i] != ' ') buff += s[i];
if(s[i] == ' ' && buff != ""){
v.push_back(buff);
buff = "";
}
}
if(buff != "") v.push_back(buff);
return v;
}
void Query(string &q, map<string, set<int> > &m){
set<int>::iterator itS;
if(m.find(q) != m.end())
for(itS = m[q].begin(); itS != m[q].end(); itS++){
printf("%07d\n", *itS);
}
else cout<<"Not Found"<<endl;
}
int main(){
// freopen("test.txt", "r", stdin);
scanf("%d", &N);
for(int i = 0; i < N; i ++){
int id;
string title, author, key, pub, year;
scanf("%d\n", &id);
getline(cin, title);
getline(cin, author);
getline(cin, key);
getline(cin, pub);
getline(cin, year);
titleM[title].insert(id);
authorM[author].insert(id);
pubM[pub].insert(id);
yearM[year].insert(id);
vector<string> keys = Explode(key);
for(int j = 0; j < keys.size(); j++){
keyM[keys[j]].insert(id);
}
}
scanf("%d", &M);
for(int i = 0; i < M; i++){
int num;
scanf("%d: ", &num);
string temp;
getline(cin, temp);
cout<<num<<": "<<temp<<endl;
switch(num){
case 1:{
Query(temp, titleM);
break;
}
case 2:{
Query(temp, authorM);
break;
}
case 3:{
Query(temp, keyM);
break;
}
case 4:{
Query(temp, pubM);
break;
}
case 5:{
Query(temp, yearM);
break;
}
}
}
return 0;
}
exprience
熟练使用getline(), getchar(), scanf() 与printf()
需要特别注意题目中出现的带有位数描述的数字,再输出时需要按位数输出。
1022 Digital Library (30)(30 point(s))的更多相关文章
- PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- pat 甲级 1022. Digital Library (30)
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
- 1022 Digital Library (30 分)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- PAT 1022 Digital Library[map使用]
1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
- 1022 Digital Library (30)(30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- 1022. Digital Library (30)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
随机推荐
- bzoj 1564 [NOI2009]二叉查找树(树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1564 [题意] 给定一个Treap,总代价为深度*距离之和.可以每次以K的代价修改权值 ...
- 20155310 2016-2017-2 《Java程序设计》第六周学习总结
20155310 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 4.1 Y86指令集体系结构 •有8个程序寄存器:%eax.%ecx.%edx.%ebx.% ...
- zzd 的割草机(Lawnmower)
评测传送门 [题目描述] 已知花坛为一个 n * m 的矩形,草只会长在某些个格子上,zzd 有一个割草机,一开始,zzd 站在(1,1)处,面向(1,m)(面向右).每次 zzd 有两个选择(耗费一 ...
- 文件操作fstream
c++文件操作详解 2009-04-16 20:46:35| 分类: C/C++|举报|字号 订阅 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ost ...
- SDL封装的系统操作(转载)
Andrew Haung bluedrum@163.com SDL封装很多操作系统的功能,为了保证SDL程序可移植性,最好尽量用这一些封装函数,哪果没有的话,才使用各种操作本地函数. 对于如何封各个 ...
- Python time()方法
from:http://www.runoob.com/python/att-time-time.html 描述 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数 ...
- MFC将二进制文件导入资源后释放
1.前言 前一篇笔记记录了怎么修改PE,此篇记录下如何利用自身的资源文件. 2.编程思路 获得资源句柄 - 获得资源文件大小 - 加载资源文件 - 锁定资源并获得其指针. 3.实践代码 1)编译以下代 ...
- MySQL 执行SQL脚本 报ERROR 1231 (42000)的解决办法【转】
今天在source mysqldump 备份文件时,发现导入的过程中报如下的错误: ERROR 1231 (42000): Variable 'time_zone' can't be set to t ...
- LVS ARP广播产生的问题和处理方式【转】
转自 LVS ARP广播产生的问题和处理方式-htckiller2010-ChinaUnix博客http://blog.chinaunix.net/uid-24960107-id-193084.htm ...
- mybatis 控制台打印sql脚本
在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...