zoukankan      html  css  js  c++  java
  • PAT甲题题解-1022. Digital Library (30)-map映射+vector

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
    http://www.cnblogs.com/chenxiwenruo/p/6789235.html
    特别不喜欢那些随便转载别人的原创文章又不给出链接的
    所以不准偷偷复制博主的博客噢~~

    题意:
    给出n本书的id、名称、作者、多个关键词、出版社、出版年
    然后给出m个查询,每个查询包含查询的种类、对应的内容
    针对每个查询,让你输出所有符合的书的id,从小到大排序,没有的话则输出No Found


    首先把每个book的信息都读取处理好,然后按照id排个序,因为最后查询需要按照id的增序输出
    重新遍历所有的book,对于book的每个信息string,通过build_map()建立一下映射
    由于有5种类别,所以分开建立映射maps[1]~maps[5]
    (因为可能会存在不同类别会出现相同的string)

    这样就把每种类型的string和vector数组book_id的索引值建立起来,vector数组存储对应的book id
    由于最多有10000个书,那么就至少有10000个title和10000个auther
    题目中还说了最多1000个不同的keywords和1000个不同的publisher
    year的范围是1000~3000,则最多有2000个
    所以映射值最多为24000个,即vector的数组大小要>24000,否则会出现段错误越界

    然后对于查询的种类k和字符串string
    找出映射值maps[k][string],如果为0,则说明找不到,输出No Found
    如果为某个值idx,那么访问vector数组book_id[idx],存储的即是对应书的id

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <map>
    #include <vector>
    #define TITLE 1
    #define AUTHER 2
    #define KEYWORDS 3
    #define PUBLISHER 4
    #define YEAR 5
    using namespace std;
    const int maxn=10000+5;
    int n;
    int cnt=0;
    map<string,int>maps[6];
    
    struct Book{
        int id;
        string title;
        string author;
        string keywords[5];
        int keynum;
        string publisher;
        string year;
        bool operator<(const Book tmp)const{
            return id<tmp.id;
        }
    }book[maxn];
    
    vector<int> book_id[maxn*3];
    
    void readKeywords(char*str,int i){
        //按照定义的分隔符d来分割字符串,对keyword进行读取
        const char *d = " ";//空格
        char *p;
        p = strtok(str,d);
        int idx=0;
        //这样做的主要原因是避免最后输出换行
        while(p)
        {
            book[i].keywords[idx]=p;
            idx++;
            p=strtok(NULL,d);  //读取下一个p
        }
        book[i].keynum=idx;
    }
    /**
    建立k、string的映射
    */
    void build_map(string s,int i,int k){
        if(maps[k][s]==0)
            maps[k][s]=++cnt;
        book_id[maps[k][s]].push_back(i);
    }
    int main()
    {
        char str[100];
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&book[i].id);
            getchar(); //这里是读取id后面的换行符
            gets(str);
            book[i].title=str;
    
            gets(str);
            book[i].author=str;
    
            gets(str);
            readKeywords(str,i);
    
            gets(str);
            book[i].publisher=str;
    
            gets(str);
            book[i].year=str;
        }
        sort(book,book+n);
        //建立映射,顺便存储书的id
        for(int i=0;i<n;i++){
            build_map(book[i].title,i,TITLE);
            build_map(book[i].author,i,AUTHER);
            for(int j=0;j<book[i].keynum;j++)
                build_map(book[i].keywords[j],i,KEYWORDS);
            build_map(book[i].publisher,i,PUBLISHER);
            build_map(book[i].year,i,YEAR);
        }
        int m;
        int kind;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d:",&kind);
            getchar();
            gets(str);
            printf("%d: %s
    ",kind,str);
            string s=str;
            int map_id=maps[kind][s];
            if(map_id==0)
                printf("Not Found
    ");
            else{
                for(int j=0;j<book_id[map_id].size();j++){
                    int bid=book_id[map_id][j];
                    printf("%07d
    ",book[bid].id);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    安装MySQL
    Apache安装与重启、启动失败
    MySQL重启
    数据库查询乱码
    MySQL数据表操作、查看mysql表的信息等
    rust学习之一:基本语法
    llvm clang的编译和安装
    Linux 性能分析60秒初步排除手段
    pahole编译过程libbpf: failed to find valid kernel BTF
    ipmitool常用指令测试大全
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/6789235.html
Copyright © 2011-2022 走看看