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))的更多相关文章

  1. PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  2. pat 甲级 1022. Digital Library (30)

    1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...

  3. 1022 Digital Library (30 分)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  4. PAT 1022 Digital Library[map使用]

    1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...

  5. 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 ...

  6. 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 ...

  7. 1022 Digital Library——PAT甲级真题

    1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...

  8. 1022 Digital Library (30)(30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  9. 1022. Digital Library (30)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

随机推荐

  1. zedboard 初使用 -- 工具篇

    <一> 安装ISE和Vivada: <二> 安装USB转UART驱动 <三> 安装USB转JTAG驱动插件 http://blog.sina.com.cn/s/bl ...

  2. 【译】第八篇 SQL Server代理使用外部程序

    本篇文章是SQL Server代理系列的第八篇,详细内容请参考原文 在这一系列的上一篇,学习了如何用SQL Server代理作业活动监视器监控作业活动和查看作业历史记录.在实时监控和管理SQL Ser ...

  3. [转]边框回归(Bounding Box Regression)详解

    https://blog.csdn.net/zijin0802034/article/details/77685438 Bounding-Box regression 最近一直看检测有关的Paper, ...

  4. 20165320 Java实验三:敏捷开发与XP实践

    实验内容: 敏捷开发与XP实践 一 实验要求: 安装alibaba 插件,解决代码中的规范问题在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Co ...

  5. 2016.6.19——Length of Last Word

    Length of Last Word 本题收获: 1.str.size()为负 2.size_t引发的死循环 3.题目思路,有时在写代码时很不清楚边界条件的输出值是什么,若为面试,一定要问清楚. 题 ...

  6. SQl 跨服务器查询脚本示例

    1.采用OPENDATASOURCE select top 10 *from OPENDATASOURCE('SQLOLEDB','Data Source=IP地址;User ID=连接用户名称;Pa ...

  7. 关于分布式存储系统中-CAP原则(CAP定理)与BASE理论比较

    CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. CA ...

  8. SHELL 中的变量

    变量的分类 系统环境变量 系统本身所有,通常为大写字母 系统变量通过 set 或 declare 指令进行查看 UDV 变量(user defined variable ) 用户创建和维护,建议大写 ...

  9. 前端程序员必知的30个Chrome扩展-[转载]

    谷歌Chrome浏览器是网络上可用的最好浏览器之一,并且自2011年11月超越了Firefox浏览器之后,已经成为了互联网上占主导地位的浏览器.今天,HTML5中国与大家分享一些实用的谷歌Chrome ...

  10. 关于 poScreenCenter 与 poDesktopCenter

    主要是窗体水平方向与垂直方向的的 居中问题,由于水平方向 没有什么,所以不探讨.而垂直方向由于底部有个工具栏,工具栏自身有个高度,所以垂直方向的居中问题,需要探讨下. 结论: poScreenCent ...