PAT 甲级 1022 Digital Library 字符串模拟

PAT 甲级 1022 Digital Library 字符串模拟
PAT 甲级 1022 Digital Library 字符串模拟
PAT 甲级 1022 Digital Library 字符串模拟
PAT 甲级 1022 Digital Library 字符串模拟

代码如下:

//模拟
#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;

int n;//书的数量
int m;//m为查询的数量

struct book{
  string number;//书的ID
  string title;//书的title
  string author;//书的author
  char keyword[5][100];//关键词
  string publisher;//出版商
  string year;//出版时间
  int cnt;//记录关键词的个数
}b[10005];

bool cmp(book a,book b){
  return a.number<b.number;
}

int main(){
  cin>>n;
  getchar();
  string keywords;
  for(int i=0;i<n;i++){
    getline(cin,b[i].number);
    getline(cin,b[i].title);
    getline(cin,b[i].author);
    getline(cin,keywords);
    int cnt=0,k=0;
    for(int j=0;j<keywords.length();j++){
        if(keywords[j]==' '){
            cnt++;
            k=0;
            continue;

        }
        b[i].keyword[cnt][k++]=keywords[j];
    }
    b[i].cnt=cnt;
    getline(cin,b[i].publisher);
    getline(cin,b[i].year);
  }
  sort(b,b+n,cmp);
  cin>>m;
  getchar();
  for(int i=0;i<m;i++){
    string temp;
    int flag;//查询的类型
    string word;
    int k=0;
    getline(cin,temp);
    flag=temp[0]-'0';
    word=temp.substr(3);
    if(flag==1){
        cout<<temp<<endl;
        bool flag2=false;
        for(int j=0;j<n;j++){
            if(b[j].title==word){
                cout<<b[j].number<<endl;
                flag2=true;
            }
        }
        if(!flag2){
            cout<<"Not Found"<<endl;
        }
    }else if(flag==2){
        cout<<temp<<endl;
        bool flag2=false;
        for(int j=0;j<n;j++){
            if(b[j].author==word){
                cout<<b[j].number<<endl;
                flag2=true;
            }
        }
        if(!flag2){
            cout<<"Not Found"<<endl;
        }
    }else if(flag==3){
        cout<<temp<<endl;
        bool flag2=false;
        for(int j=0;j<n;j++){
            for(int h=0;h<=b[j].cnt;h++){
                if(b[j].keyword[h]==word){
                     cout<<b[j].number<<endl;
                     flag2=true;
                     break;
                }
            }
        }
        if(!flag2){
            cout<<"Not Found"<<endl;
        }
    }else if(flag==4){
        cout<<temp<<endl;
        bool flag2=false;
        for(int j=0;j<n;j++){
            if(b[j].publisher==word){
                cout<<b[j].number<<endl;
                flag2=true;
            }
        }
        if(!flag2){
            cout<<"Not Found"<<endl;
        }
    }else if(flag==5){
        cout<<temp<<endl;
        bool flag2=false;
        for(int j=0;j<n;j++){
            if(b[j].year==word){
                cout<<b[j].number<<endl;
                flag2=true;
            }
        }
        if(!flag2){
            cout<<"Not Found"<<endl;
        }
    }
  }
  return 0;
}