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;
}
时间: 2024-10-08 04:59:30

PAT 1022. Digital Library的相关文章

PAT 甲级 1022 Digital Library

https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigne

PAT Advanced 1022 Digital Library (30分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are s

1022. Digital Library (30)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are s

1022. Digital Library (30) -map -字符串处理

题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you

1022 Digital Library (30)(30 point(s))

problem A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, y

1022 Digital Library

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are s

PTA (Advanced Level)1022 Digital Library

Digital Library A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a r

PAT A 1022. Digital Library (30)【结构体排序检索】

https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5

PAT (Advanced Level) 1022. Digital Library (30)

简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct X { string id; c