题目链接:
题目:
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
解题思路:
第一种思路:模拟
1、创建一个书籍结构体,用来保存书中的所有关键信息。
2、将书籍按照id排序(注意可能出现71的情况,我们要使用 %07d进行0的添加)。
3、使用自己编写的getWords处理括号(或者使用c++ stl getline 进行的读取)。
4、根据用户查询信息,进行分类匹配结果,输出结果即可。
第二种思路:map映射查询,将主题、内容等信息分别放到一个map<string,set<int>>中,用以进行查询。
解题代码:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int MAXSIZE = 10001;
const int wordsize = 1000;
struct Book{
int id;
char *ti;
char *author;
char *keyWords;
char *publisher;
char *year;
friend bool operator < (Book b1,Book b2){
return b1.id<b2.id;
}
};
Book *bs;
int n,m;
char *getWord(){
char *a = new char[1000];
char ch;
int sum = 0;
while((ch=getchar())!=NULL){
if(ch=='\n') break;
//if(ch!=' '&&ch!=':'){
a[sum++] = ch;
// }
}
a[sum] = '\0';
return a;
}
//bool cmp()
void init(){
cin>>n;
bs = new Book[n];
for(int i = 0 ;i<n;i++){
cin>>bs[i].id;
getchar();
bs[i].ti = getWord();
// getchar();
bs[i].author = getWord();
//cout<<bs[i].author<<endl;
bs[i].keyWords = getWord();
bs[i].publisher = getWord();
bs[i].year = getWord();
}
sort(bs,bs+n);
}
struct Query{
int num;
char *content;
queue<int> bookId;
};
/**
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
*/
//找子串
bool getSub(char *p,char *s){
int len = strlen(p);
char word[100] ;
int st = 0;
for(int i = 0 ;i<len;i++){
if(p[i]!=' '){
char ch = p[i];
//if(ch>='A'&&ch<='Z') ch = ch-'A'+'a';
word[st++] = p[i];
}else{
word[st] = '\0';
if(strcmp(word,s)==0){
return true;
}
st = 0;
}
}
if(st!=0){
word[st] = '\0';
if(strcmp(word,s)==0){
return true;
}
}
return false;
}
queue<int> getBooK(int i,char *content){
queue<int> q ;
switch(i){
case 1:
for(int i =0;i<n;i++){
if(strcmp(bs[i].ti,content)==0){
q.push(bs[i].id);
}
}
break;
case 2:
for(int i =0;i<n;i++){
if(strcmp(bs[i].author,content)==0){
q.push(bs[i].id);
}
}
break;
case 3:
for(int i =0;i<n;i++){
if(getSub(bs[i].keyWords,content)){
q.push(bs[i].id);
}
}
break;
case 4:
for(int i =0;i<n;i++){
if(strcmp(bs[i].publisher,content)==0){
q.push(bs[i].id);
}
}
break;
case 5:
for(int i =0;i<n;i++){
if(strcmp(bs[i].year,content)==0){
q.push(bs[i].id);
}
}
break;
}
return q;
}
void query(){
int m;
cin>>m;
Query *qs = new Query[m];
for(int i = 0 ; i <m;i++){
char s[5];
cin>>s;
qs[i].num = s[0] - '0'+0;
getchar();
qs[i].content = getWord();
//进行查询
}
for(int i = 0 ;i<m;i++){
qs[i].bookId = getBooK(qs[i].num,qs[i].content);
}
//输出
queue<Query> nq;
for(int i = 0 ;i<m;i++){
if(!qs[i].bookId.empty()){
printf("%07d",qs[i].num);
cout<<": "<<qs[i].content<<endl;
while(!qs[i].bookId.empty()){
int j = qs[i].bookId.front();
cout<<j<<endl;
qs[i].bookId.pop();
}
}else{
printf("%07d",qs[i].num);
cout<<": "<<qs[i].content<<endl;
cout<<"Not Found"<<endl;
}
}
}
int main()
{
init();
query();
//cout<<getWord();
}