PAT甲级1022 Digital Library (30分)|C++实现

一、题目描述

原题链接
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:

在这里插入图片描述

​​Output Specification:

在这里插入图片描述

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

二、解题思路

这道题不难,但有点复杂。我们可以把每本书设置成一个结构体,包括题目、作者、关键词、出版社、年份和书的id。所有书的信息存放在一个结构体数组b[maxn]中。id是一个7位数字,我们可以用int类型表示。现在题目的要求是给出除了id以外的信息,要我们输出满足条件的书的id。显然,这是一种字符串对整数的一个映射,我们可以用map,题目时间限制为1200ms,是相当够用的,但是保险一点,我们这里用unordered_map<string, vector<int> >。

我们这里输入的keywords是有空格的,所以我们要对这些字符串做一个预处理,我们定义了一个函数Process(int id, string keywords)来做这件事。其余部分的操作都很简单了,细节请看代码注释。

三、AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<unordered_map>
using namespace std;
const int maxn = 10010;
struct Book
{
  string title, author, keywords, publisher, year;
  int id;
}b[maxn];   //存放书的数组
unordered_map<string, vector<int> > Tit, Au, Key, Pub, Year;
void Process(int id, string keywords)
{
  int cnt = 0, st = 0;
  for(int i=0; i<keywords.size(); i++)
  {
    if(keywords[i] == ' ' || i==keywords.size()-1)
    {
      if(i == keywords.size()-1)
      	Key[keywords.substr(st, cnt+1)].push_back(id);
      else
        Key[keywords.substr(st, cnt)].push_back(id);
      cnt = 0;
      st = i+1;
    }   //对关键词的分割处理
    else
      cnt++;
  }
  return;
}
int main()
{
  int N, M, query;
  string que;
  scanf("%d", &N);
  for(int i=0; i<N; i++)
  {
   	scanf("%d", &b[i].id);
    getchar();  //吸收上一个回车
    getline(cin, b[i].title);   //我们要输入空格,所以用getline(cin, ...)
    getline(cin, b[i].author);
    getline(cin, b[i].keywords);
    getline(cin, b[i].publisher);
    getline(cin, b[i].year);
    Process(b[i].id, b[i].keywords);    //处理当前书本的关键词
    Tit[b[i].title].push_back(b[i].id);
    Au[b[i].author].push_back(b[i].id);
    Pub[b[i].publisher].push_back(b[i].id);
    Year[b[i].year].push_back(b[i].id);
  }
  scanf("%d", &M);
  for(int i=0; i<M; i++)
  {
    scanf("%d: ", &query);
    getline(cin, que);
    printf("%d: %s\n", query, que.c_str());
    if(query == 1)
    {
      if(Tit[que].size() == 0)
        printf("Not Found\n");
      else
      {
        sort(Tit[que].begin(), Tit[que].end());
        for(int j=0; j<Tit[que].size(); j++)
          printf("%07d\n", Tit[que][j]);
      }
    }
    else if(query == 2)
    {
      if(Au[que].size() == 0)
        printf("Not Found\n");
      else
      {
        sort(Au[que].begin(), Au[que].end());
        for(int j=0; j<Au[que].size(); j++)
          printf("%07d\n", Au[que][j]);
      }
    }
    else if(query == 3)
    {
      if(Key[que].size() == 0)
        printf("Not Found\n");
      else
      {
        sort(Key[que].begin(), Key[que].end());
        for(int j=0; j<Key[que].size(); j++)
          printf("%07d\n", Key[que][j]);
      }
    }
    else if(query == 4)
    {
      if(Pub[que].size() == 0)
        printf("Not Found\n");
      else
      {
        sort(Pub[que].begin(), Pub[que].end());
        for(int j=0; j<Pub[que].size(); j++)
          printf("%07d\n", Pub[que][j]);
      }
    }
    else
    {
      if(Year[que].size() == 0)
        printf("Not Found\n");
      else
      {
        sort(Year[que].begin(), Year[que].end());
        for(int j=0; j<Year[que].size(); j++)
          printf("%07d\n", Year[que][j]);
      }
    }
  }
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值