首页 > 代码库 > PAT 1022. Digital Library
PAT 1022. Digital Library
完全考输入输出
#include <iostream>#include <cstdio>#include <cstdlib>#include <vector>#include <string>#include <unordered_map>#include <algorithm>using namespace std;unordered_map<string, vector<int> > idx_title;unordered_map<string, vector<int> > idx_author;unordered_map<string, vector<int> > idx_keyword;unordered_map<string, vector<int> > idx_publisher;unordered_map<string, vector<int> > idx_year;unordered_map<string, vector<int> >* idx[6] = {NULL, &idx_title, &idx_author, &idx_keyword, &idx_publisher, &idx_year};void idx_add_book(unordered_map<string, vector<int> >& idx, const string& key, int book_id) { auto iter = idx.find(key); if (iter == idx.end()) { iter = idx.insert(make_pair(key, vector<int>())).first; } iter->second.push_back(book_id);}int main() { int N, M; scanf("%d", &N); char ibuf[100] = {0}; int bid = 0; for (int i=0; i<N; i++) { scanf("%d", &bid); getchar(); // read title scanf("%[^\n]", ibuf); idx_add_book(idx_title, string(ibuf), bid); getchar(); // read author scanf("%[^\n]", ibuf); idx_add_book(idx_author, string(ibuf), bid); getchar(); // read keywords for (;;) { scanf("%[^ \n]", ibuf); idx_add_book(idx_keyword, string(ibuf), bid); if (getchar() == ‘\n‘) { break; } } // read publisher scanf("%[^\n]", ibuf); idx_add_book(idx_publisher, string(ibuf), bid); getchar(); // read year scanf("%[^\n]", ibuf); idx_add_book(idx_year, string(ibuf), bid); getchar(); } scanf("%d", &M); string buf; for (int i=0; i<M; i++) { int idx_type = 0; scanf("%d", &idx_type); getchar(); getchar(); if (idx_type < 1 || idx_type > 5) { break; } getline(cin, buf); printf("%d: %s\n", idx_type, buf.c_str()); auto dict = idx[idx_type]; if (dict == NULL) { break; } auto iter = dict->find(buf); if (iter == dict->end()) { printf("Not Found\n"); continue; } auto v = iter->second; sort(v.begin(), v.end()); int len = v.size(); for (int i=0; i<len; i++) { printf("%07d\n", v[i]); } } return 0;}
PAT 1022. Digital Library
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。