博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6789235.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~

题意:
给出n本书的id、名称、作者、多个关键词、出版社、出版年
然后给出m个查询,每个查询包含查询的种类、对应的内容
针对每个查询,让你输出所有符合的书的id,从小到大排序,没有的话则输出No Found

首先把每个book的信息都读取处理好,然后按照id排个序,因为最后查询需要按照id的增序输出
重新遍历所有的book,对于book的每个信息string,通过build_map()建立一下映射
由于有5种类别,所以分开建立映射maps[1]~maps[5]
(因为可能会存在不同类别会出现相同的string)

这样就把每种类型的string和vector数组book_id的索引值建立起来,vector数组存储对应的book id
由于最多有10000个书,那么就至少有10000个title和10000个auther
题目中还说了最多1000个不同的keywords和1000个不同的publisher
year的范围是1000~3000,则最多有2000个
所以映射值最多为24000个,即vector的数组大小要>24000,否则会出现段错误越界

然后对于查询的种类k和字符串string
找出映射值maps[k][string],如果为0,则说明找不到,输出No Found
如果为某个值idx,那么访问vector数组book_id[idx],存储的即是对应书的id

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define TITLE 1
#define AUTHER 2
#define KEYWORDS 3
#define PUBLISHER 4
#define YEAR 5
using namespace std;
const int maxn=+;
int n;
int cnt=;
map<string,int>maps[]; struct Book{
int id;
string title;
string author;
string keywords[];
int keynum;
string publisher;
string year;
bool operator<(const Book tmp)const{
return id<tmp.id;
}
}book[maxn]; vector<int> book_id[maxn*]; void readKeywords(char*str,int i){
//按照定义的分隔符d来分割字符串,对keyword进行读取
const char *d = " ";//空格
char *p;
p = strtok(str,d);
int idx=;
//这样做的主要原因是避免最后输出换行
while(p)
{
book[i].keywords[idx]=p;
idx++;
p=strtok(NULL,d); //读取下一个p
}
book[i].keynum=idx;
}
/**
建立k、string的映射
*/
void build_map(string s,int i,int k){
if(maps[k][s]==)
maps[k][s]=++cnt;
book_id[maps[k][s]].push_back(i);
}
int main()
{
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&book[i].id);
getchar(); //这里是读取id后面的换行符
gets(str);
book[i].title=str; gets(str);
book[i].author=str; gets(str);
readKeywords(str,i); gets(str);
book[i].publisher=str; gets(str);
book[i].year=str;
}
sort(book,book+n);
//建立映射,顺便存储书的id
for(int i=;i<n;i++){
build_map(book[i].title,i,TITLE);
build_map(book[i].author,i,AUTHER);
for(int j=;j<book[i].keynum;j++)
build_map(book[i].keywords[j],i,KEYWORDS);
build_map(book[i].publisher,i,PUBLISHER);
build_map(book[i].year,i,YEAR);
}
int m;
int kind;
scanf("%d",&m);
for(int i=;i<m;i++){
scanf("%d:",&kind);
getchar();
gets(str);
printf("%d: %s\n",kind,str);
string s=str;
int map_id=maps[kind][s];
if(map_id==)
printf("Not Found\n");
else{
for(int j=;j<book_id[map_id].size();j++){
int bid=book_id[map_id][j];
printf("%07d\n",book[bid].id);
}
}
}
return ;
}

PAT甲题题解-1022. Digital Library (30)-map映射+vector的更多相关文章

  1. 1022. Digital Library (30) -map -字符串处理

    题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...

  2. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

  3. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  4. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  5. PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

    题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...

  6. PAT甲题题解-1091. Acute Stroke (30)-BFS

    题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写 ...

  7. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

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

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

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

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

随机推荐

  1. Linux之因BASH造成的键盘错误和环境问题

    对于Linux我们习惯使用/bin/bash.并且大多数人操作在Centos系统上,但是仍有不少人在ubuntu上使用,两个操作系统大同小异.都是使用了Linux内核.接下来就来讲讲我使用过程中两个系 ...

  2. 利用MSF的MS08_067模块攻击windows server 2003 SP2中文版系统

    一.测试环境 攻击机:kali(NMAP+MSF) 靶机:windows server 2003 SP2 中文版 利用漏洞:MS08_067 二.漏洞描述 MS08-067漏洞的全称为“Windows ...

  3. 51nod 贪心算法题集

    2070 最小罚款: 题意:初始有n元,每个任务有2个参数:t和w,<=t时刻前完成任务才可避免造成损失w.问:如何安排才能尽可能避免损失?一个任务执行时间是一个单位时间. 分析:任务按时间排个 ...

  4. Eclipse 中怎样自动格式化代码?

    首先 有一个 检查代码风格的工具叫checkstyle,具体怎么下载,请自行百度.. 当你在eclipse安装好 checkstyle后,对于使用google标准的人来说,选择一个项目,右键,点击ch ...

  5. [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景

    如何理解uiautomator里面的 child, child_by_text, sibling,我们借助android原生的uiautomatorviewer抓取的控件来进行理解 以如下图进行详细讲 ...

  6. 解决Windows Server2008 R2中IE开网页时弹出阻止框

    使用Windows Server2008,用IE打开网站时会弹出“Internet Explorer增强安全配置正在阻止来自下列网站的此应用程序中的内容”的对话框.如下图所示: 2011-10-14_ ...

  7. opengl渲染管线梳理

    opengl渲染管线梳理 http://www.cnblogs.com/zhanglitong/p/3238989.html 坐标系变换和矩阵 http://www.cppblog.com/guoji ...

  8. 启动报错:Access denied for user 'root'@'localhost' (using password:YES)

    项目启动报错:Access denied for user 'root'@'localhost' (using password:YES) 原因:root帐户默认不开放远程访问权限,所以需要修改一下相 ...

  9. 编译有哪些阶段,动态链接和静态链接的区别 c++

    预处理—->编译—->汇编—->链接 预处理:编译器将C程序的头文件编译进来,还有宏的替换 编译:这个阶段编译器主要做词法分析.语法分析.语义分析等,在检查无错误后后,把代码翻译成汇 ...

  10. 1、pyspider安装

    系统环境: centos6.6.python2.7 经测试,python2.6安装的pyspider会导致webui无法正常访问 参考博文: http://cuiqingcai.com/2443.ht ...