https://www.patest.cn/contests/pat-a-practise/1022
Direct simulation,
Input, sort by ID, search
#include <iostream> #include <string> #include <algorithm>using namespace std;struct book//book structure { String id;string title;string author;int k;//keyword number string key[5];string pub;string year;}; bool cm (const book &b1,const book &B2);//Sort by ID int main () {int n,m,i,j,k;string temp_s;book temp_b,*bks;cin>& Gt;n;bks=new Book [N];cin.get (); String input filter Enter with space for (i=0;i<n;i++)//input book information {getline (cin,bks[i].id); getline (Cin,bks[i].title); Getline (Cin,bks[i]. author); getline (cin,temp_s); for (J=0,k=0;j<temp_s.size (); j + +) {if (temp_s[j]!= ') bks[i].key[k]+=temp_s[j]; elsek++;} Bks[i].k=k+1;getline (cin,bks[i].pub); getline (cin,bks[i].year);} Sort (bks,bks+n,cm);//Sort by ID cin>>m;int cl;int flag;//flag Whether there is a matching book for (i=0;i<m;i++) {Cin>>cl;cin.get ();//Skip ":" Cin.get ();//Skip "" Flag=0;getline (cin,temp_s);cout<<cl<< ":" <<temp_s<<endl;for (J =0;j<n;j++) {switch (CL) {case 1:if (bks[j].title==temp_s) {cout<<bks[j].id<<endl; flag=1; }break;case 2:if (bks[j].author==temp_s) {cout<<bks[j].id<<endl; flag=1; }break;case 3:for (k=0;k<bks[j].k;k++) if (bks[j].key[k]==temp_s) {cout<<bks[j].id<<endl;flag=1;} Break;case 4:if (bks[j].pub==temp_s) {cout<<bks[j].id<<endl; flag=1; }break;case 5:if (bks[j].year==temp_s) {cout<<bks[j].id<<endl; flag=1; }break;}} if (flag==0) cout<< "not found\n";} return 0;} bool cm (const book &b1,const book &B2) {return b1.id<b2.id;}
PAT A 1022. Digital Library (30) "Structure sorting search"