title: PTA 1071 Speech Patterns (25分)
date: 2020-10-02 15:43:59
tags:
- Map
categories: - [刷题,PAT]
强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
PS 今天也要加油鸭
题目原文
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 supposed to output the resulting books, sorted in increasing order of their ID’s.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:
- Line #1: the 7-digit ID number;
- Line #2: the book title – a string of no more than 80 characters;
- Line #3: the author – a string of no more than 80 characters;
- Line #4: the key words – each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
- Line #5: the publisher – a string of no more than 80 characters;
- Line #6: the published year – a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.
After the book information, there is a line containing a positive integer M (≤1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:
- 1: a book title
- 2: name of an author
- 3: a key word
- 4: name of a publisher
- 5: a 4-digit number representing the year
Output Specification:
For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print Not Found
instead.
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
生词如下:
Digital 数字的
abstracts 摘要,抽象
题目大意:
给你N本书
然后给你这书的一些信息,分别是
id ,书名,作者,关键字–有多个.出版社.出版年份
然后再给N个查询,1 是查书名 2 是查作者,3 是查关键字,4是查出版社,5是查出版月份
要你给出查询到的id
思路如下:
我们用
map<string set<int>>
来做,不过要注意关键字的keywords的录入.
还有就是getline 之前要读取多余的\n
char c =getchar();
代码如下:
#include<iostream>
#include<map>
#include<set>
#include<string>
using namespace std;
void query(map < string, set<int>>& mp, string& str); //输出函数
int main(void) {
map<string, set<int>> title_id,author_id,key_id, pub_id,pubyear_id;
int N=0,id=0;
string t, key;
cin >> N;
bool flag = false;
for (int i = 0; i < N; ++i) {
cin >> id;
char c = getchar(); //接受掉换行
getline(cin, t); title_id[t].insert(id);
getline(cin, t); author_id[t].insert(id);
while (cin >> key) {
key_id[key].insert(id);
char c = getchar();
if (c == '\n') break;
}
getline(cin, t); pub_id[t].insert(id);
getline(cin, t); pubyear_id[t].insert(id);
}
cin >> N;
char c = getchar(); //接受掉换行
for (int i = 0; i < N; ++i) {
getline(cin, t);
if (t[0] == '1') { //Book title就是书本的标签
cout << t << endl;
t.erase(t.begin(), t.begin() + 3);
query(title_id, t);
}
else if (t[0] == '2') { //author_id
cout << t << endl;
t.erase(t.begin(), t.begin() + 3);
query(author_id, t);
}
else if (t[0] == '3') { //key_id
cout << t << endl;
t.erase(t.begin(), t.begin() + 3);
query(key_id, t);
}
else if (t[0] == '4') { //pub_id
cout << t << endl;
t.erase(t.begin(), t.begin() + 3);
query(pub_id, t);
}
else if (t[0] == '5') {
cout << t << endl;
t.erase(t.begin(), t.begin() + 3);
query(pubyear_id, t);
}
}
return 0;
}
void query(map < string, set<int>>& mp, string& str) {
if (mp.find(str) == mp.end()) printf("Not Found\n");
else {
for (set<int>::iterator it = mp[str].begin(); it != mp[str].end(); it++) {
printf("%07d\n", *it);
}
}
}
这题柳神的思路和我的差不多,就不贴了.有兴趣的,自己去柳神的Blog上去翻一番.
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.