PAT (Advanced Level) 1022. Digital Library (30)

简单模拟题。

写的时候注意一些小优化,小心TLE。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
using namespace std;

struct X
{
    string id;
    char title[90]; int Title;
    char author[90]; int Author;
    vector<int>key;
    char publisher[90]; int Publisher;
    int year;
}s[10000+10];
int n,m;

map<string,int>Tit,Aut,Key,Pub;
vector<string>ans;
int tot_Tit=0,tot_Aut=0,tot_Key=0,tot_Pub=0;

bool cmp(const X&a,const X&b)
{
    return a.id<b.id;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        cin>>s[i].id; getchar();
        gets(s[i].title); if(Tit[s[i].title]==0) Tit[s[i].title]=++tot_Tit; s[i].Title=Tit[s[i].title];
        gets(s[i].author); if(Aut[s[i].author]==0) Aut[s[i].author]=++tot_Aut; s[i].Author=Aut[s[i].author];
        char tmp[1000]; gets(tmp);
        int len=strlen(tmp);
        string q;
        for(int j=0;j<=len;j++)
        {
            if(tmp[j]==‘ ‘||tmp[j]==‘\0‘)
            {
                if(Key[q]==0) Key[q]=++tot_Key;
                s[i].key.push_back(Key[q]);
                q.clear();
            }
            else q=q+tmp[j];
        }
        gets(s[i].publisher); if(Pub[s[i].publisher]==0) Pub[s[i].publisher]=++tot_Pub; s[i].Publisher=Pub[s[i].publisher];
        scanf("%d",&s[i].year);
    }

    sort(s+1,s+1+n,cmp);

    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        char op[5]; scanf("%s",op); getchar();
        char h[100]; gets(h);
        printf("%s %s\n",op,h);

        if(op[0]==‘1‘)
        {
            int num=Tit[h];
            for(int i=1;i<=n;i++)
                if(s[i].Title==num) ans.push_back(s[i].id);
        }
        else if(op[0]==‘2‘)
        {
            int num=Aut[h];
            for(int i=1;i<=n;i++)
                if(s[i].Author==num) ans.push_back(s[i].id);
        }
        else if(op[0]==‘3‘)
        {
            int num=Key[h];
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<s[i].key.size();j++)
                {
                    if(s[i].key[j]==num)
                    {
                        ans.push_back(s[i].id);
                        break;
                    }
                }
            }
        }
        else if(op[0]==‘4‘)
        {
            int num=Pub[h];
            for(int i=1;i<=n;i++)
                if(s[i].Publisher==num) ans.push_back(s[i].id);
        }
        else if(op[0]==‘5‘)
        {
            int num=0;
            for(int i=0;h[i];i++) num=num*10+h[i]-‘0‘;
            for(int i=1;i<=n;i++)
                if(s[i].year==num) ans.push_back(s[i].id);
        }
        if(ans.size()==0) printf("Not Found\n");
        else
        {
            for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
            ans.clear();
        }
    }
    return 0;
}
时间: 2024-12-16 10:34:31

PAT (Advanced Level) 1022. Digital Library (30)的相关文章

PTA (Advanced Level)1022 Digital Library

Digital Library A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a r

PAT Advanced 1022 Digital Library (30分)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are s

1022. Digital Library (30)

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are s

1022. Digital Library (30) -map -字符串处理

题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you

1022 Digital Library (30)(30 point(s))

problem A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, y

PAT:1022. Digital Library (30) (部分正确,错最后两个)

1 #include<stdio.h> 2 #include<string.h> 3 #include<map> 4 #include<string> 5 #include<set> 6 #include<iostream> 7 using namespace std; 8 int n; //n本书 9 //标题.作者.关键字.出版社.出版年份与ID的映射 10 map<string,set<int>> mpT

PAT A 1022. Digital Library (30)【结构体排序检索】

https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5

PAT甲题题解-1022. Digital Library (30)-map映射+vector

博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789235.html特别不喜欢那些随便转载别人的原创文章又不给出链接的所以不准偷偷复制博主的博客噢~~ 题意:给出n本书的id.名称.作者.多个关键词.出版社.出版年然后给出m个查询,每个查询包含查询的种类.对应的内容针对每个查询,让你输出所有符合的书的id,从小到大排序,没有的话则输出No Found 首先把每个book的信息都读取处理好,然后按照id排个序

PAT (Advanced Level) 1026. Table Tennis (30)

情况比较多的模拟题. 交了50发的样子才AC......AC之后我的天空星星都亮了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct