1. 程式人生 > 實用技巧 >1022 Digital Library (30分) hash模擬

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;
}