【1022】Digital Library (30 分)

简介: 【1022】Digital Library (30 分)【1022】Digital Library (30 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<string>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include <set>
using namespace std; 
//key:map<string,set<int>>
//5个map变量分别建立书名、作者、关键字、出版社及出版年份与id的映射关系
map<string,set<int>> mpTitle,mpAuthor,mpKey,mpPub,mpYear;
void query(map<string,set<int>>& mp,string& str){ //在mp中查找str
  if(mp.find(str) == mp.end())  printf("Not Found\n"); //找不到
  else{  //找到str
    for(set<int>::iterator it=mp[str].begin() ; it!=mp[str].end();it++){
      printf("%07d\n",*it); //输出str对应的所有id
    }
  }
}   
int main(){   
  int n,m,id,type;
  string title,author,key,pub,year;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&id);  //id
    char c=getchar(); //接收掉id后面的换行!虽然前面scanf以换行结束
    getline(cin,title);  //读入书名titile
    mpTitle[title].insert(id); //把id加入titile对应的集合中
    getline(cin,author);
    mpAuthor[author].insert(id); //把id加入author对应的集合中
    while(cin >> key){ //每次读入!单个!关键词key
      mpKey[key].insert(id);  //把id加入key对应的集合中
      c=getchar(); //接收关键词key之后的字符
      if(c == '\n')  break;  //如果是换行,说明关键词输入结束
    }
    getline(cin,pub);  //输入出版社pub
    mpPub[pub].insert(id);  //把id加入pub对应的集合中
    getline(cin,year);  //输入年份year
    mpYear[year].insert(id);  //把id加入year对应的集合中
  }
  string temp;  //查询次数
  scanf("%d",&m);  
  for(int i=0;i<m;i++){
    scanf("%d: ",&type);  //查询类型
    //注意读上面的数字后scanf结束
    getline(cin,temp);
    cout << type <<": "<<temp<<endl;  //输出类型和该字符串
    if(type==1) query(mpTitle,temp); //查询书名对应的所有id
    else if(type == 2) query(mpAuthor,temp);  //作者
    else if(type == 3) query(mpKey,temp);  //关键字
    else if(type == 4) query(mpPub,temp);  //出版社
    else query(mpYear,temp);
  }
  system("pause");
    return 0;   
}
相关文章
|
10月前
|
存储 C++
【PAT甲级 - C++题解】1022 Digital Library
【PAT甲级 - C++题解】1022 Digital Library
41 0
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
67 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
76 0
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
101 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
90 0
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
117 0
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
96 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
110 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
96 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
78 0