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()=='\n') 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\n";
        else {
            for(auto it=lib[r][t].begin();it!=lib[r][t].end();++it)
                printf("%07d\n",*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()=='\n') 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\n",lib[i].id);
            }
        }
        if(f1==0) cout<<"Not Found\n";
    }
    return 0;
}
posted @ 2020-08-11 15:51  liuyongliu  阅读(96)  评论(0编辑  收藏  举报