zoukankan      html  css  js  c++  java
  • 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("%[^
    ]", ibuf);
            idx_add_book(idx_title, string(ibuf), bid);
            getchar();
            // read author
            scanf("%[^
    ]", ibuf);
            idx_add_book(idx_author, string(ibuf), bid);
            getchar();
            // read keywords
            for (;;) {
                scanf("%[^ 
    ]", ibuf);
                idx_add_book(idx_keyword, string(ibuf), bid);
                if (getchar() == '
    ') {
                    break;
                }
            }
            // read publisher
            scanf("%[^
    ]", ibuf);
            idx_add_book(idx_publisher, string(ibuf), bid);
            getchar();
            // read year
            scanf("%[^
    ]", 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
    ", 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
    ");
                continue;
            }
            auto v = iter->second;
            sort(v.begin(), v.end());
            int len = v.size();
            for (int i=0; i<len; i++) {
                printf("%07d
    ", v[i]);
            }
        }
    
        return 0;
    }
  • 相关阅读:
    Cisco 交换机配置的基本命令
    Mysql读写分离方案-Amoeba环境部署记录
    centos7下部署zabbix3.4+grafana
    Docker
    Linux 安装源码软件
    mysql 日志
    mysql导出导入数据
    mysql 数据库的备份和还原
    Mysql 数据库管理
    英语单词
  • 原文地址:https://www.cnblogs.com/lailailai/p/4100912.html
Copyright © 2011-2022 走看看