zoukankan      html  css  js  c++  java
  • 1022 Digital Library (30分) hash模拟

    题目

    https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336

    题意

    模拟图书馆,查找

    Sample Input:

    3
    1111111
    The Testing Book
    Yue Chen
    test code debug sort keywords
    ZUCS Print
    2011
    3333333
    Another Testing Book
    Yue Chen
    test code sort keywords
    ZUCS Print2
    2012
    2222222
    The Testing Book
    CYLL
    keywords debug book
    ZUCS Print2
    2011
    6
    1: The Testing Book
    2: Yue Chen
    3: keywords
    4: ZUCS Print
    5: 2011
    3: blablabla

    Sample Output:

    1: The Testing Book
    1111111
    2222222
    2: Yue Chen
    1111111
    3333333
    3: keywords
    1111111
    2222222
    3333333
    4: ZUCS Print
    1111111
    5: 2011
    1111111
    2222222
    3: blablabla
    Not Found

    思路

    为什么用结构体数组模拟不行呢,总有几个点过不去
    还是得看柳神的解法~

    code AC

    #include <bits/stdc++.h>
    using namespace std;
    map<string,set<int>>lib[6];
    int main()
    {
        int n; cin>>n;
        while(n--)
        {
            int id; cin>>id;
            string temp;
            getchar();
            getline(cin,temp);
            lib[1][temp].insert(id);
            getline(cin,temp);
            lib[2][temp].insert(id);
            while(cin>>temp)
            {
                lib[3][temp].insert(id);
                if(getchar()=='
    ') break;
            }
            getline(cin,temp);
            lib[4][temp].insert(id);
            getline(cin,temp);
            lib[5][temp].insert(id);
        }
        int m; cin>>m;
        while(m--)
        {
            int r;
            scanf("%d: ",&r);
            string t;
            getline(cin,t);
            cout<<r<<": "<<t<<endl;
            if(lib[r][t].size()==0) cout<<"Not Found
    ";
            else {
                for(auto it=lib[r][t].begin();it!=lib[r][t].end();++it)
                    printf("%07d
    ",*it);
            }
        }
        return 0;
    }
    

    code 数组模拟 过不去~~~

    #include <bits/stdc++.h>
    using namespace std;
    
    struct book {
        int id;
        string title,author;
        vector<string>key;
        string publisher,year;
    }temp;
    vector<book>lib;
    bool cmp(book x,book y) {return x.id<y.id;}
    
    int march(string s,int x,int y)
    {
        if(y==1) return lib[x].title==s;
        else if(y==2) return lib[x].author==s;
        else if(y==4) return lib[x].publisher==s;
        else if(y==5) return lib[x].year==s;
        else
            for(int i=0;i<lib[x].key.size();++i) {
                if(lib[x].key[i]==s) return 1;
            }
        return 0;
    }
    
    int main()
    {
        int N; cin>>N;
        getchar();
        for(int i=1;i<=N;++i)
        {
            string s;
            cin>>temp.id;
            getchar();
            getline(cin,temp.title);
            getline(cin,temp.author);
            while(cin>>s)
            {
                temp.key.push_back(s);
                if(getchar()=='
    ') break;
            }
            getline(cin,temp.publisher);
            getline(cin,temp.year);
            lib.push_back(temp);
        }
        sort(lib.begin(),lib.end(),cmp);
    
        int M; cin>>M;
        getchar();
        while(M--) {
            string q;
            int y;
            scanf("%d: ",&y);
            getline(cin,q);
            cout<<y<<": "<<q<<endl;
    
            int f1=0;
            for(int i=0;i<lib.size();++i)//挨个匹配q1,q2
            {
                int f=march(q,i,y);
    
                if(f>0) {
                    f1=1;
                    printf("%07d
    ",lib[i].id);
                }
            }
            if(f1==0) cout<<"Not Found
    ";
        }
        return 0;
    }
    
  • 相关阅读:
    JQuery替换空字符串和正则表达式校验时间格式
    数据库升级,给某张表增加字段,防止重复升级时sql脚本报错
    将jar包安装到本地repository中
    springCloud集成常用组件(持续更新)
    记一次springboot配置事务@transactional失效的事故
    分布式锁实现(Redis和zookeeper)
    springCloud集成zookeper
    zookeeper学习相关
    springboot集成activeMq
    springboot集成redis
  • 原文地址:https://www.cnblogs.com/liuyongliu/p/13476101.html
Copyright © 2011-2022 走看看