pat 1022 digital library
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
//利用map键的唯一性
//建立key为关键词,value为id的map map<string, vector<int>> name;
map<string, vector<int>> author;
map<string, vector<int>> keyWord;
map<string, vector<int>> press;
map<string, vector<int>> year;//对出版年同样做string对待 int main(void)
{
//ifstream fin("data.txt"); int num, bookID;
cin>>num;
string temp, nameTemp, authorTemp, keyWordLine,
keyWordTemp, pressTemp, yearTemp;
for(int i=; i<num; i++)
{
cin>>bookID;
getline(cin, temp);//这里多余的输入去掉回车符
getline(cin, nameTemp);
name[nameTemp].push_back(bookID);
//map下标操作:
//1.如果存在key,则返回对应的value
//2.如果key不存在,插入key getline(cin, authorTemp);
author[authorTemp].push_back(bookID); //输入一行字符串并且逐个分解单词
getline(cin, keyWordLine);
istringstream stream(keyWordLine);
while(stream >> keyWordTemp)
keyWord[keyWordTemp].push_back(bookID); getline(cin, pressTemp);
press[pressTemp].push_back(bookID); getline(cin, yearTemp);
year[yearTemp].push_back(bookID);
} int queryNum;
cin>>queryNum;
string query;
getline(cin, temp);//同样去掉整型后面的回车符 for(int i=; i<queryNum; i++)
{
getline(cin, query);
string queryText(query.begin()+, query.end());//拷贝关键词
map<string, vector<int>>::iterator it = name.find(queryText);
if(it != name.end())//关键词存在
{
cout<<query<<endl;
sort(it->second.begin(), it->second.end());//按id顺序
for(int j=; j<it->second.size(); j++)
cout<<setfill('')<<setw()<<it->second[j]<<endl;
continue;//按照题意,关键词是name keyword author等中的一种
//查询到则进行下一次查询
} it = author.find(queryText);
if(it != author.end())
{
cout<<query<<endl;
sort(it->second.begin(), it->second.end());
for(int j=; j<it->second.size(); j++)
cout<<setfill('')<<setw()<<it->second[j]<<endl;
continue;
} it = keyWord.find(queryText);
if(it != keyWord.end())
{
cout<<query<<endl;
sort(it->second.begin(), it->second.end());
for(int j=; j<it->second.size(); j++)
cout<<setfill('')<<setw()<<it->second[j]<<endl;
continue;
} it = press.find(queryText);
if(it != press.end())
{
cout<<query<<endl;
sort(it->second.begin(), it->second.end());
for(int j=; j<it->second.size(); j++)
cout<<setfill('')<<setw()<<it->second[j]<<endl;
continue;
} it = year.find(queryText);
if(it != year.end())
{
cout<<query<<endl;
sort(it->second.begin(), it->second.end());
for(int j=; j<it->second.size(); j++)
cout<<setfill('')<<setw()<<it->second[j]<<endl;
continue;
} //未找到
cout<<query<<endl<<"Not Found"<<endl;
} return ;
}
pat 1022 digital library的更多相关文章
- PAT 1022 Digital Library[map使用]
1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...
- 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 ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
- 1022 Digital Library (30 分)
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 ...
- 1022. Digital Library (30)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- 1022. Digital Library (30) -map -字符串处理
题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...
随机推荐
- 自定义MVC框架---第二章
模型层的封装 模型层封装的原则 介绍: 模型层,也就是Model这一层,用来封装对数据库操作的封装 由于现在主流的编程思想还是OOP面向对象编程,也就是说项目的基本单位就是一个一个类文件,那么如何使用 ...
- SQLAlchemy框架用法详解
介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DBAPI之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据API执行SQL并获取执 ...
- Redis 数据结构与内存管理策略(上)
Redis 数据结构与内存管理策略(上) 标签: Redis Redis数据结构 Redis内存管理策略 Redis数据类型 Redis类型映射 Redis 数据类型特点与使用场景 String.Li ...
- Saving James Bond(dijk)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...
- Zabbix如何实现批量监控端口状态
引言 ------------------------------------------------------------------------------------------------- ...
- zlib1.2.11静态编译
1.进入官网http://zlib.net/,下载且解压zlib1211.zip: 2. 打开已解压的zlib-1.2.11目录,找到win32文件夹 3.将Makefile.msc复制到上一层,也就 ...
- Dockerfile中CMD和ENTRYPOINT的区别
当启动一个容器时,CMD和ENTRYPOINT都可以用来执行启动命令.但它们的具体用法还是有一些区别: 1. Dockerfile必须至少指定CMD或者ENTRYPOINT其中的一个. 2. ENTR ...
- 对python-rrdtool模块的浅研究。
一,python-rrdtool模块安装. 切记!!! 这个rrdtool模块,在windows环境下安装太费劲,就是因为没安装上所以现在改成了在ubuntu环境下开发,原来没有体会过,现在可真是体会 ...
- Sql Server——约束
约束是什么: 每个人都在网站或者APP上注册过账号,在注册账号时会限制用户名.密码等格式,如果格式不对就不能注册.在数据库中我们可以通过约束来进行限制,超过约束范围的数据就不能写入. 约束的种类: 主 ...
- UE4 保存为bitmap
TArray<FColor> colorData; colorData.Init(FColor(0, 0, 255, 255), 1920 * 1080); for (int ...