[pat]1022. Digital Library (30)

https://www.patest.cn/contests/pat-a-practise/1022

#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<set>
using namespace std;

typedef struct{
	string id;
	string name;
	string author;
	vector<string> kws;
	string publisher;
	string year;
}Book;

vector<Book> vbook;
int n;

void split2string(vector<string>& res, string s, string split){
	s = s + split;
	int a, start = 0;
	
	while( start < s.length() && (a = s.find(split, start)) != s.npos ){
		if(a != start){
			res.push_back(s.substr(start, a-start));
		}
		start = a + 1;
	}
}

bool a_is_in_b(vector<string>& a, vector<string>& b){
	for(int i = 0; i < a.size(); i++){
		if(find(b.begin(), b.end(), a[i]) == b.end())
			return false;
	}
	return true;
}

int main(){
	
	
	cin>>n;
	for(int i = 0; i < n; i++){
		Book b;
		cin>>b.id;
		getchar();
		getline(cin, b.name);
		getline(cin, b.author);

		string kws;
		getline(cin, kws);
		split2string(b.kws, kws, " ");

		getline(cin, b.publisher);
		cin>>b.year;
		vbook.push_back(b);
	}
	int k;
	cin>>k;
	getchar();
	vector<vector<string>> res(k+1);
	for(int i = 1; i <= k; i++){
		string q;
		getline(cin, q);
		res[i].push_back(q);
		string query = q.substr(3, q.length() - 3);
		switch(q[0]){
		case '1':
			for(int j = 0; j < vbook.size(); j++){
				if(query == vbook[j].name)
					res[i].push_back(vbook[j].id);
			}
			break;
		case '2':
			for(int j = 0; j < vbook.size(); j++){
				if(query == vbook[j].author)
					res[i].push_back(vbook[j].id);
			}
			break;
		case '3':
			for(int j = 0; j < vbook.size(); j++){
				vector<string> kws;
				split2string(kws, query, " ");
				if(a_is_in_b(kws, vbook[j].kws))
					res[i].push_back(vbook[j].id);
			}
			break;
		case '4':
			for(int j = 0; j < vbook.size(); j++){
				if(query == vbook[j].publisher)
					res[i].push_back(vbook[j].id);
			}
			break;
		case '5':
			for(int j = 0; j < vbook.size(); j++){
				if(query == vbook[j].year)
					res[i].push_back(vbook[j].id);
			}
			break;
		default:
			break;
		}
		if(res[i].size() == 1)
			res[i].push_back("Not Found");
	}

	for(int i = 1; i <= k; i++){
		vector<string>& vs = res[i];
		cout<<vs[0]<<endl;
		sort(vs.begin()+1, vs.end());
		for(int j = 1; j < vs.size(); j++)
			cout<<vs[j]<<endl;
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(C++,pat,OJ)