PAT A1022 Digital Library

PAT A1022 Digital Library

在这里插入图片描述在这里插入图片描述

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
  • 思路 1:
    应查询,需要分别用title, author, keywords, publisher, pubTime来查询书id =》用map来分别建立这5个string与书id的映射,由于输出要求按id递增,map的second的类型选择set(去掉重复,且有序)

  • code 1:

#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <ctype.h>
using namespace std;
unordered_map<string, set<int> > title, author, keywords, publisher, pubTime;
int n, id;
string keys[110];
string tit, aut, key, puber, pubt, tmp;
// 麻烦了-.-!! 
void query(int i, string str){
	switch(i){
		case 1:
			if(title.find(str) == title.end()) printf("Not Found\n");
			else
				for(auto it = title[str].begin(); it != title[str].end(); ++it)
					printf("%07d\n", *it);
			break;
		case 2:
			if(author.find(str) == author.end()) printf("Not Found\n");
			else
				for(auto it = author[str].begin(); it != author[str].end(); ++it)
					printf("%07d\n", *it);
			break;
		case 3:
			if(keywords.find(str) == keywords.end()) printf("Not Found\n");
			else
				for(auto it = keywords[str].begin(); it != keywords[str].end(); ++it)
					printf("%07d\n", *it);
			break;
		case 4:
			if(publisher.find(str) == publisher.end()) printf("Not Found\n");
			else
				for(auto it = publisher[str].begin(); it != publisher[str].end(); ++it)
					printf("%07d\n", *it);
			break;
		case 5:
			if(pubTime.find(str) == pubTime.end()) printf("Not Found\n");
			else
				for(auto it = pubTime[str].begin(); it != pubTime[str].end(); ++it)
					printf("%07d\n", *it);
			break; 
	}
}
int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &id);
		getchar();
		getline(cin, tit);
		title[tit].insert(id);
		getline(cin, aut);
		author[aut].insert(id);
		//!!!TIPS 1: 分割word: 对key的处理 
		//Way1: 
/*		
		getline(cin, key);
		int idex = 0, idex1 = 0, len = key.size();
		while(idex < len){
			tmp.clear();
		  //while(isalpha(key[idex]))	//Weong 1:keyword可能不是字母
			while(key[idex] != ' ' && idex < len)
				tmp += key[idex++];	//不能key[idex++] 
			if(!tmp.empty())
				keys[idex1++] = tmp;
			idex++;
		}
		for(int j = 0; j < idex1; ++j){
			keywords[keys[j]].insert(id);
		}
*/ 
		//Way2: 
		while(cin >> key){
			keywords[key].insert(id);
			char c = getchar(); 
			if(c == '\n') break;
		} 
		
		getline(cin, puber);
		publisher[puber].insert(id);
		getline(cin, pubt);
		pubTime[pubt].insert(id);
	}
	int nq, queNum;
	string que;
	scanf("%d", &nq);
	for(int i = 0; i < nq; ++i){
		scanf("%d: ", &queNum);
		getline(cin, que);
		cout << queNum << ": " << que <<endl;
		//Way2: 
/*
		getline(cin, que);
		int queNum = que[0] - '0';
		que = que.substr(3);
*/
		query(queNum, que);
	}
	return 0;
}
  • code 1-2:优化query(),直接将map作为参数传入
...
void query(unordered_map<string, set<int> >& mp, string& s){
	if(mp.find(s) == mp.end()) printf("Not Found\n");
	else{
		for(auto it = mp[s].begin(); it != mp[s].end(); ++it)
			printf("%07d\n", *it);	//必须加07格式化输出 
	}
}
int main(){
	...
	for(int i = 0; i < nq; ++i){
		...
		switch(queNum){
			case 1: query(title, que);
				break;
			case 2: query(author, que);
				break;
			case 3: query(keywords, que);
				break;
			case 4: query(publisher, que);
				break;
			case 5: query(pubTime, que);
				break;
		}
	}
	return 0;
}
  • 思路:用map数组来存储unordered_map<string, set<int> > mp[6];unordered_map<string, vector<int> > mp[6];,通过下标来查询对应主题,省去了查询时的判断逻辑
    另:

  • T2 code:

#include <bits/stdc++.h>
using namespace std;

//unordered_map<string, vector<int> > mp[6];
unordered_map<string, set<int> > mp[6];

int main(){
	int n;
	string tmp;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		int id;
		scanf("%d", &id); getchar();
		//分词:Plan 1 
		for(int j = 1; j <= 5; ++j){
			if(j == 3){
				while(cin >> tmp){
					mp[j][tmp].insert(id); 
					char c = getchar();
					if(c == '\n') break;
				}
			}else{		
				getline(cin, tmp);
				mp[j][tmp].insert(id); 
			} 
		}
	}
	scanf("%d", &n);
	for(int i = 0; i < n; ++i){
		int q_id;
		scanf("%d: ", &q_id);
		printf("%d: ", q_id);
		getline(cin, tmp);	
		cout << tmp <<endl;
		if(mp[q_id][tmp].size() == 0) printf("Not Found\n");
		else{
//			sort(mp[q_id][tmp].begin(), mp[q_id][tmp].end());
//			for(int j = 0; j < mp[q_id][tmp].size(); ++j){
//				printf("%07d\n", mp[q_id][tmp][j]);
//			}
			for(auto it = mp[q_id][tmp].begin(); it != mp[q_id][tmp].end(); ++it){
				printf("%07d\n", *it);
			} 
		}
	}
	return 0;
}
  • 分词: Plan 2:双指针法
		for(int j = 1; j <= 5; ++j){
			getline(cin, tmp);
			if(j == 3){	
			//Plan 1:
				int l = 0, r = 0;
				while(r < tmp.size()){
					while(r < tmp.size() && tmp[r] != ' ') r++;
					string word = tmp.substr(l, r-l);
					mp[j][word].insert(id); 
					r++;
					l = r;
				}
			}else mp[j][tmp].insert(id); 
		}
发布了277 篇原创文章 · 获赞 5 · 访问量 7864
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览