第九天PAT-A1022 Digital Library字符串读入MAP映射模拟题

5 篇文章 0 订阅

A1022

Description:

给出读者的任意查询,求查询结果的书籍,按照书籍ID升序排列

Input:

  • 一个测试一个样例;
  • 每个样例,首行一个正整数N<=1e4,代表书籍总数,然后是N本书籍的详细信息;
  • 保证每本书只属于一个作者,每本书不超过5个关键字,关键字共计不超过1000个,不超过1000个出版社;
  • 书籍信息输出后,接着一行:M代表查询数<=1000,接下来M行,每行按照规定格式进行查询;

Output:

  • 每个查询,首先打印出初始查询内容,然后输出查询结果的所有书籍ID,按升序排列,每本书一行。如果没有书籍被查询到,输出Not Found

算法思想:

  • 模拟题,按照题目表述做就OK;
  • 带空格的字符串输入法总结:getline(cin, str); //str是要接受字符串的string
  • 复杂变量类型,如STL类,传参速度较慢,在传参时加引用符可有效提升速度;
  • 第一次提交,眼泪都快下来了,PAT上第一次大满贯…贴个图纪念一​下🙊

大满贯

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e4+5;
typedef pair<string, set<int> > PSV;
map<string, set<int> >titlem;
map<string, set<int> >authorm;
map<string, set<int> >keym;
map<string, set<int> >pubm;
map<string, set<int> >yearm;
int n;
void query(map<string, set<int> >&mp, string& qur){
    if(mp.find(qur) == mp.end()) printf("Not Found\n");
    else{
        for(auto it = mp[qur].begin(); it != mp[qur].end(); it++)
            printf("%07d\n", *it);
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
    scanf("%d", &n);
    int id;
    string ti, au, key, pub, year;
    for(int i = 0; i < n; i++){
        scanf("%d", &id);
        char c = getchar();
        getline(cin, ti);
        titlem[ti].insert(id);
        getline(cin, au);
        authorm[au].insert(id);
        while(cin>>key){
            keym[key].insert(id);
            c = getchar();
            if(c == '\n') break;
        }
        getline(cin, pub);
        pubm[pub].insert(id);
        getline(cin, year);
        yearm[year].insert(id);
    }
    int q, t;
    string qur;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        scanf("%d: ", &t);
        getline(cin, qur);
        cout<<t<<": "<<qur<<endl;
        if(t == 1) query(titlem, qur);
        else if(t == 2) query(authorm, qur);
        else if(t == 3) query(keym, qur);
        else if(t == 4) query(pubm, qur);
        else query(yearm, qur);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值