【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;   
}
相关文章
|
安全 Unix Shell
读书笔记系列 - Operating Systems: Three Easy Pieces - Virtualization - Chapter 5: Process API
读书笔记系列 - Operating Systems: Three Easy Pieces - Virtualization - Chapter 5: Process API
91 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
107 0
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
94 0
|
存储 容器
SAP QM 高阶之Physical Sample Management(1)
SAP QM 高阶之Physical Sample Management(1)
SAP QM 高阶之Physical Sample Management(1)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
121 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
115 0
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
PAT (Advanced Level) Practice - 1107 Social Clusters(30 分)
144 0
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
119 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
103 0