首页 > 代码库 > pat 1022 digital library
pat 1022 digital library
1 #include <iostream> 2 #include <sstream> 3 #include <string> 4 #include <vector> 5 #include <map> 6 #include <fstream> 7 #include <algorithm> 8 #include <iomanip> 9 using namespace std; 10 //利用map键的唯一性 11 //建立key为关键词,value为id的map 12 13 map<string, vector<int>> name; 14 map<string, vector<int>> author; 15 map<string, vector<int>> keyWord; 16 map<string, vector<int>> press; 17 map<string, vector<int>> year;//对出版年同样做string对待 18 19 int main(void) 20 { 21 //ifstream fin("data.txt"); 22 23 int num, bookID; 24 cin>>num; 25 string temp, nameTemp, authorTemp, keyWordLine, 26 keyWordTemp, pressTemp, yearTemp; 27 for(int i=0; i<num; i++) 28 { 29 cin>>bookID; 30 getline(cin, temp);//这里多余的输入去掉回车符 31 getline(cin, nameTemp); 32 name[nameTemp].push_back(bookID); 33 //map下标操作: 34 //1.如果存在key,则返回对应的value 35 //2.如果key不存在,插入key 36 37 getline(cin, authorTemp); 38 author[authorTemp].push_back(bookID); 39 40 //输入一行字符串并且逐个分解单词 41 getline(cin, keyWordLine); 42 istringstream stream(keyWordLine); 43 while(stream >> keyWordTemp) 44 keyWord[keyWordTemp].push_back(bookID); 45 46 getline(cin, pressTemp); 47 press[pressTemp].push_back(bookID); 48 49 getline(cin, yearTemp); 50 year[yearTemp].push_back(bookID); 51 } 52 53 int queryNum; 54 cin>>queryNum; 55 string query; 56 getline(cin, temp);//同样去掉整型后面的回车符 57 58 for(int i=0; i<queryNum; i++) 59 { 60 getline(cin, query); 61 string queryText(query.begin()+3, query.end());//拷贝关键词 62 map<string, vector<int>>::iterator it = name.find(queryText); 63 if(it != name.end())//关键词存在 64 { 65 cout<<query<<endl; 66 sort(it->second.begin(), it->second.end());//按id顺序 67 for(int j=0; j<it->second.size(); j++) 68 cout<<setfill(‘0‘)<<setw(7)<<it->second[j]<<endl; 69 continue;//按照题意,关键词是name keyword author等中的一种 70 //查询到则进行下一次查询 71 } 72 73 it = author.find(queryText); 74 if(it != author.end()) 75 { 76 cout<<query<<endl; 77 sort(it->second.begin(), it->second.end()); 78 for(int j=0; j<it->second.size(); j++) 79 cout<<setfill(‘0‘)<<setw(7)<<it->second[j]<<endl; 80 continue; 81 } 82 83 it = keyWord.find(queryText); 84 if(it != keyWord.end()) 85 { 86 cout<<query<<endl; 87 sort(it->second.begin(), it->second.end()); 88 for(int j=0; j<it->second.size(); j++) 89 cout<<setfill(‘0‘)<<setw(7)<<it->second[j]<<endl; 90 continue; 91 } 92 93 it = press.find(queryText); 94 if(it != press.end()) 95 { 96 cout<<query<<endl; 97 sort(it->second.begin(), it->second.end()); 98 for(int j=0; j<it->second.size(); j++) 99 cout<<setfill(‘0‘)<<setw(7)<<it->second[j]<<endl;100 continue;101 }102 103 it = year.find(queryText);104 if(it != year.end())105 {106 cout<<query<<endl;107 sort(it->second.begin(), it->second.end());108 for(int j=0; j<it->second.size(); j++)109 cout<<setfill(‘0‘)<<setw(7)<<it->second[j]<<endl;110 continue;111 }112 113 //未找到114 cout<<query<<endl<<"Not Found"<<endl;115 }116 117 return 0;118 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。