PAT1022 Digital Library (30)(map的使用)
很简单的题,就是考察一个map的使用,不过这题有个陷阱就是最后输出的id必须是7位的,我做这题的时候用到了string的分割,其实不需要,直接用getchar()就可以判断结束条件,注意使用getline时要吃掉\n
#include<string>
#include<cstdlib>
#include<vector>
#include<stack>
#include<queue>
#include<utility>
#include<map>
#include<set>
#include<cstdlib>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<functional>
#include<iostream>
#define INF 0x6ffff
using namespace std;
map<string, set<int>> title;
map<string, set<int>> author;
map<string, set<int>> keyword;
map<string, set<int>> publisher;
map<string, set<int>> year;
vector<string> split(const string &str, const string &pattern)
{
//const char* convert to char*
char * strc = new char[strlen(str.c_str()) + 1];
strcpy(strc, str.c_str());
vector<string> resultVec;
char* tmpStr = strtok(strc, pattern.c_str());
while (tmpStr != NULL)
{
resultVec.push_back(string(tmpStr));
tmpStr = strtok(NULL, pattern.c_str());
}
delete[] strc;
return resultVec;
}
void print(set<int> &res) {
if (res.empty())
cout << "Not Found" << endl;
else {
for (auto x : res)
printf("%07d\n", x);
}
}
int main() {
int n,m;
cin >> n;
while (n--) {
int id;
string t, a, k, p,y;
cin >> id;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, t);
getline(cin, a);
getline(cin, k);
getline(cin, p);
cin >> y;
vector<string> key = split(k, " ");
for (auto x : key)
keyword[x].insert(id);
title[t].insert(id);
author[a].insert(id);
publisher[p].insert(id);
year[y].insert(id);
}
cin >> m;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (m--) {
int num;
scanf("%d: ", &num);
string query;
getline(cin, query);
cout << num << ": " << query << endl;
switch (num) {
case 1:print(title[query]); break;
case 2:print(author[query]); break;
case 3:print(keyword[query]); break;
case 4:print(publisher[query]); break;
case 5:print(year[query]); break;
}
}
return 0;
}
网上的代码,其中对一行字符串分别以空格进行分割,最后用\n进行判断的处理可以学习一下:
#include <iostream>
#include <map>
#include <set>
using namespace std;
map<string, set<int> > title, author, key, pub, year;
void query(map<string, set<int> > &m, string &str) {
if(m.find(str) != m.end()) {
for(auto it = m[str].begin(); it != m[str].end(); it++)
printf("%07d\n", *it);
} else
cout << "Not Found\n";
}
int main() {
int n, m, id, num;
scanf("%d", &n);
string ttitle, tauthor, tkey, tpub, tyear;
for(int i = 0; i < n; i++) {
scanf("%d\n", &id);
getline(cin, ttitle);
title[ttitle].insert(id);
getline(cin, tauthor);
author[tauthor].insert(id);
while(cin >> tkey) { //这里可以学习一下
key[tkey].insert(id);
char c = getchar();
if(c == '\n') break;
}
getline(cin, tpub);
pub[tpub].insert(id);
getline(cin, tyear);
year[tyear].insert(id);
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d: ", &num);
string temp;
getline(cin, temp);
cout << num << ": " << temp << "\n";
if(num == 1) query(title, temp);
else if(num == 2) query(author, temp);
else if(num == 3) query(key, temp);
else if(num == 4) query(pub,temp);
else if(num ==5) query(year, temp);
}
return 0;
}