两个坑。

一个是一直用的malloc不行了。因为malloc分配的是固定大小,之前做的题没遇到过是因为一般string都不长(malloc分配string为24个Byte),这次直接报段错误,呢们了半天。

第二个很无语,就是ID会有0,当int处理的时候把0消掉了,会有两个case不通过。

#include <iostream>
#include <vector>
#include <sstream>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
struct Book{
string id;
string title;
string author;
vector<string> keyWords;
string publish;
int year;
};
int n,m;
string tmp;
string line;
vector<Book> bookSet;
map<string,vector<int> > mmT;
map<string,vector<int> > mmA;
map<string,vector<int> > mmK;
map<string,vector<int> > mmP;
map<int,vector<int> > mmY;
bool cmp(int a,int b){
return bookSet[a].id<bookSet[b].id;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
Book b;
cin>>b.id;
getline(cin,line);//cin没有读取回车
getline(cin,line);b.title=line;mmT[b.title].push_back(i);
getline(cin,line);b.author=line;mmA[b.author].push_back(i);
getline(cin,line);
stringstream ss(line);
while(ss>>tmp){
b.keyWords.push_back(tmp);
mmK[tmp].push_back(i);
}
getline(cin,line);b.publish=line;mmP[b.publish].push_back(i);
cin>>b.year;mmY[b.year].push_back(i);
bookSet.push_back(b);
}
int queN;
cin>>m;
for(int i=0;i<m;i++)
{
scanf("%d: ",&queN);
getline(cin,tmp);
int yea;
vector<int> logN;
switch(queN){
case 1:logN=mmT[tmp];break;
case 2:logN=mmA[tmp];break;
case 3:logN=mmK[tmp];break;
case 4:logN=mmP[tmp];break;
case 5:yea=stoi(tmp);logN=mmY[yea];break;
default:break;
}
printf("%d: ",queN);cout<<tmp<<endl;
sort(logN.begin(),logN.end(),cmp);
vector<int>::iterator ii=logN.begin();
while(ii!=logN.end()){
cout<<bookSet[(*ii)].id<<endl;
ii++;
}
if(logN.size()==0){
cout<<"Not Found"<<endl;
} } }

PAT1022. Digital Library (30)的更多相关文章

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

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

  2. PAT1022.:Digital Library

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

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

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

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

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

  5. 1022 Digital Library (30 分)

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

  6. 1022. Digital Library (30)

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

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

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

  8. 1022 Digital Library (30)(30 point(s))

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

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

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

随机推荐

  1. 【BZOJ1863】[Zjoi2006]trouble 皇帝的烦恼 二分+DP

    [BZOJ1863][Zjoi2006]trouble 皇帝的烦恼 Description 经过多年的杀戮,秦皇终于统一了中国.为了抵御外来的侵略,他准备在国土边境安置n名将军.不幸的是这n名将军羽翼 ...

  2. EasyNVR浏览器无插件直播在Linux系统下将录像文件与EasyNVR可执行文件分离运行的方案

    问题背景 在工控机上运行EasyNVR,WEB访问出现设备在线,但是视频没有快照和无法正常直播: 问题原因分析 通过上工控机发现是由于磁盘空间被占满导致的软件运行收到影响. 解决问题分析 由于录像文件 ...

  3. 第七课 nodejs请求响应

    1 server.js 接收请求接收请求参数 和接收完成需要对request增加两个监听事件 var http = require('http');var url = require('url');f ...

  4. text files and binary files

    https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件

  5. <2013 08 13> TeX and LaTeX, some introduction

    1.  TeX是Donald E. Knuth教授的精心杰作,它是个功能非常强大的幕后排版系统,含有弹性很大,而且很低阶的排版语言.含有九百多条指令,用Pascal语言(的一个子集)写成. 2.  T ...

  6. python系列十:python3函数

    #!/usr/bin/python #-*-coding:gbk-*- '''函数的简单规则:    函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ().    任何传入参数和自变量必 ...

  7. 浅析僵尸进程&孤儿进程

    0x01 前言 此文出自:https://www.cnblogs.com/Anker/p/3271773.html 博文主要用unix/linux举例,但道理没问题的同样有助于在Python中理解僵尸 ...

  8. k8s 常用命令

    [root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 1h v1.8.1 node1 ...

  9. 根据URL请求 返回XML字符串

    public static string GetHttpResponse(string url) { string content = ""; // Create a new Ht ...

  10. net mvc 利用NPOI导入导出excel

    1.导出Excel : 首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...