PAT甲级1022 Digital Library
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
题意:
每一本书有一个id, 书名,作者,至多五个关键字,一个出版社名,出版社年份。
现在根据给定的书名或作者或关键字或出版社名或年份,按照id字典序大小输出符合条件的书。
思路:
对每一个属性都用map维护。
一个坑点是,前面说书的年份一定在1000-3000之间,但是查询的时候的年份不一定满足,而且这里输出的时候也要满足4位。测试点1就是和年份有关的。
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, m;
const int maxn = 1e4 + ; struct node{
string id;
string title;
string author;
string keyword[];
string publisher;
int year;
int keynum = ;
}book[maxn]; map<string, int>titlemp;
map<string, int>authormp;
map<string, int>keymp;
map<string, int>publishermp;
map<int, string>idmp;
int titletot, authortot, keytot, publishertot, idtot; vector<int> title[maxn], author[maxn], keyword[], publisher[], year[]; bool cmp(int a, int b)
{
int ida = stoi(idmp[a]), idb = stoi(idmp[b]);
return ida < idb;
} int main()
{
scanf("%d", &n);
for(int i = ; i < n; i++){
getchar();getline(cin, book[i].id);
idmp[i] = book[i].id; getline(cin, book[i].title);
if(titlemp.find(book[i].title) == titlemp.end()){
titlemp[book[i].title] = titletot++;
}
title[titlemp[book[i].title]].push_back(i);
getline(cin, book[i].author);
if(authormp.find(book[i].author) == authormp.end()){
authormp[book[i].author] = authortot++;
}
author[authormp[book[i].author]].push_back(i);
char ch;
while(){
cin>>book[i].keyword[book[i].keynum];
//cout<<book[i].keyword[book[i].keynum]<<endl;
if(keymp.find(book[i].keyword[book[i].keynum]) == keymp.end()){
keymp[book[i].keyword[book[i].keynum]] = keytot++;
}
keyword[keymp[book[i].keyword[book[i].keynum++]]].push_back(i);
ch = getchar();
if(ch == '\n')break;
}
getline(cin, book[i].publisher);
if(publishermp.find(book[i].publisher) == publishermp.end()){
publishermp[book[i].publisher] = publishertot++;
}
publisher[publishermp[book[i].publisher]].push_back(i);
cin>>book[i].year;
year[book[i].year - ].push_back(i);
} scanf("%d", &m);
while(m--){
int type;
scanf("%d: ", &type);
string s;
int id, y;
//getchar();
switch(type){
case :
getline(cin, s);
printf("1: ");cout<<s<<endl;
if(titlemp.find(s) == titlemp.end()){
printf("Not Found\n");
}
else{
id = titlemp[s];
sort(title[id].begin(), title[id].end(), cmp);
for(int i = ; i < title[id].size(); i++){
cout<<idmp[title[id][i]]<<endl;
}
}
break;
case :
getline(cin, s);
printf("2: ");cout<<s<<endl;
if(authormp.find(s) == authormp.end()){
printf("Not Found\n");
}
else{
id = authormp[s];
sort(author[id].begin(), author[id].end(), cmp);
for(int i = ; i < author[id].size(); i++){
cout<<idmp[author[id][i]]<<endl;
}
}
break;
case :
getline(cin, s);
printf("3: ");cout<<s<<endl;
if(keymp.find(s) == keymp.end()){
printf("Not Found\n");
}
//cout<<id<<endl;
else{
id = keymp[s];
sort(keyword[id].begin(), keyword[id].end(), cmp);
for(int i = ; i < keyword[id].size(); i++){
cout<<idmp[keyword[id][i]]<<endl;
}
}
break;
case :
getline(cin, s);
printf("4: ");cout<<s<<endl;
if(publishermp.find(s) == publishermp.end()){
printf("Not Found\n");
}
else{
id = publishermp[s];
sort(publisher[id].begin(), publisher[id].end(), cmp);
for(int i = ; i < publisher[id].size(); i++){
cout<<idmp[publisher[id][i]]<<endl;
}
}
break;
case :
cin>>s;
printf("5: ");cout<<s<<endl;
y = stoi(s);
if( y< || y > || year[y - ].size() == ){
printf("Not Found\n");
}
else{
sort(year[y - ].begin(), year[y - ].end(), cmp);
for(int i = ; i < year[y - ].size(); i++){
cout<<idmp[year[y - ][i]]<<endl;
}
}
break;
}
}
return ;
}
PAT甲级1022 Digital Library的更多相关文章
- pat 甲级 1022. Digital Library (30)
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 A Digital Library cont ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT甲级——A1022 Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT A 1022. Digital Library (30)【结构体排序检索】
https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
- PAT 1022 Digital Library[map使用]
1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...
- 1022 Digital Library (30 分)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
随机推荐
- .net使用Aspose.Words进行Word替换操作的实现代码
DLL文件下载 示例: Aspose.Words.Document doc = new Aspose.Words.Document(TempFile); Aspose.Words.DocumentBu ...
- iTOP-iMX6UL全能板-linux-usb-wifi的使用
本文档介绍的是在本文档介绍的是在 Linux 系统环境下iTOP-imx6ul全能版 usb wifi 连接路由器上网 实验调试步骤.我们使用的是 imx6ul 全功能底板. 1 硬件 本文档测试使用 ...
- 通俗易懂的vuex-demo
在main.js引入store.js
- Django之form表单
https://www.cnblogs.com/liwenzhou/p/8747872.html 1. form组件的介绍 之前web开发的模式,以注册为例: 1. 要有一个注册的页面,页面里面要有f ...
- PHP 【二】
EOF EOF(heredoc)是一种在命令行shell(如sh.csh.ksh.bash.PowerShell和zsh)和程序语言(像Perl.PHP.Python和Ruby)里定义一个字符串的方法 ...
- Django目录
app和ORM的操作与介绍 框架简介 中间件 form介绍 Django自带用户认证 cookie和session 模版 模板2 ORM操作 所有ORM操作(第二版) Django请求生命周期 Dja ...
- nginx 配置身份验证 http_auth_basic_module
ngx_http_auth_basic_module模块实现访问必须输入用户名和密码 正确情况向访问,这为我们一些重要资源访问增添了一道安全锁. 语法: auth_basic_user_fil ...
- apache 配置反向代理 设置
1.下载 安装 下载地址:http://httpd.apache.org/download.cgi 将apache 安装到某个目录中 修改conf/http.conf文件 修改配置文件端口 (端口为 ...
- Java泛型用法总结
普通泛型 class Point< T>{ // 此处可以随便写标识符号,T是type的简称 private T var ; // var的类型由T指定,即:由外部指定 public T ...
- Java Web环境搭建
——————————JavaWeb环境搭建 先下载JDK, Tomcat 7.0 安装JDK后,配置环境变量,此处可参考博客: https://www.cnblogs.com/smyhvae/p/37 ...