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 ...
随机推荐
- windows环境下git的环境变量配置
1.从官网下载git这个软件.msi格式,然后安装. 2.找到你的git的安装目录,并记录下来 3.配置环境变量:在path里加入—— ;你的git的安装目录\bin;你的git的安装目录\libe ...
- oracle06
1. Oracle的体系结构 - 了解 1.1. Oracle数据库和Oracle实例 Oracle 服务器软件部分由两大部分组成, Oracle 数据库 和 Oracle 实例. 两者的解释如下: ...
- JDOM生成XML文档的一般方法
由于DOM提供的生成XML的方法不够直观,而且要用到各种繁琐的注解,鉴于此可借助第三方库-----JDOM生成XML文档.具体操作方式如下: import java.io.FileOutputStre ...
- Linux XOR.DDoS样本取证特征与清除
一.取证特征 1)获取进程ID 使用top命令,查看占用内存率最高的十位随机名称进程名(示例:进程名pygdykcrqf) 2)获取进程对应路径 Linux 在启动一个进程时,系统会在/proc下创建 ...
- python基础之命名空间
前言 命名空间通俗的理解就是对象或变量的作用范围,在python中分为局部命令空间.模块命名空间和build-in全局命名空间. 局部命名空间 局部命名空间即在一个函数或一个类中起作用的变量或引用的字 ...
- linux用户权限 -> 系统特殊权限
set_uid 运行一个命令的时候,相当于这个命令的所有者,而不是执行者的身份. suid的授权方法 suid 权限字符s(S),用户位置上的x位上设置. 授权方法: passwd chmod u+s ...
- shell 数组基础->
数组其实也算是变量, 传统的变量只能存储一个值, 但数组可以存储多个值. 普通数组:只能使用整数 作为数组索引 [有序 0 1 2 3 4 ]关联数组:可以使用字符串 作为数组索引 [无序 name ...
- PHP在引号前面添加反斜杠的原因及PHP去除反斜杠的办法
昨天用PHP做了个读写html文档的小程序,本地测试正常但是传到网站后发现,提交内容保存的时候会自动在双引号前面增加一个反斜杠“\”,而且每保存一次增加一个反斜杠,很是郁闷. 当然做这个只是为了参加电 ...
- C# byte[] 转换16进制字符串
1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...
- html-示例代码
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html" xml ...