【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; }

若不给自己设限,则人生中就没有限制你发挥的藩篱。

【1022】Digital Library (30 分)

相关文章:

你感兴趣的文章:

标签云: