您的位置:首页 > 其它

浙大 PAT Advanced level 1022. Digital Library (30)

2016-05-13 18:10 543 查看
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 (<=10000) 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

题目描述比较复杂,但思路很清晰。本题的测试点的数据量都不大,采取依次遍历查询的方式可以AC。如果数据量较大,要考虑先排序然后二分查找。

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

int temp[10005];

typedef struct records
{
unsigned int ID;
char title[85];
char author[85];
int keyWordNum;
char keyWords[5][11];
char publisher[85];
char publishYear[6];
}records;

// 格式转换
int printBufferToKeyword(char *buffer, records *lib)
{
int keyWordNum = 0;
int startpos = 0;
int endpos = -1;
int index = 0;
int i;

do
{
++endpos;
if (' ' == buffer[endpos] || '\0' == buffer[endpos])
{
for (i = 0; i != endpos-startpos; ++i)
{
lib->keyWords[index][i] = buffer[startpos+i];
}
lib->keyWords[index][i] = '\0';
startpos = endpos+1;
++index;
++keyWordNum;
}
}while ('\0' != buffer[endpos]);

return keyWordNum;
}

// 返回匹配成功的个数
int titleMatch(records *lib, int num, char *title)
{
int index = 0;
for (int i = 0; i != num; ++i)
{
if (0 == strcmp(title, lib[i].title))
{
temp[index++] = lib[i].ID;
}
}
sort(temp, temp+index);
return index;
}
// 返回匹配成功的个数
int authorMatch(records *lib, int num, char *author)
{
int index = 0;
for (int i = 0; i != num; ++i)
{
if (0 == strcmp(author, lib[i].author))
{
temp[index++] = lib[i].ID;
}
}
sort(temp, temp+index);
return index;
}
// 返回匹配成功的个数
int keyWordMatch(records *lib, int num, char *keyWord)
{
int index = 0;
for (int i = 0; i != num; ++i)
{
for (int j = 0; j != lib[i].keyWordNum; ++j)
{
if (0 == strcmp(keyWord, lib[i].keyWords[j]))
{
temp[index++] = lib[i].ID;
break;
}
}
}
sort(temp, temp+index);
return index;
}
// 返回匹配成功的个数
int publisherMatch(records *lib, int num, char *publisher)
{
int index = 0;
for (int i = 0; i != num; ++i)
{
if (0 == strcmp(publisher, lib[i].publisher))
{
temp[index++] = lib[i].ID;
}
}
sort(temp, temp+index);
return index;
}
// 返回匹配成功的个数
int publishYearMatch(records *lib, int num, char *publishYear)
{
int index = 0;
for (int i = 0; i != num; ++i)
{
if (0 == strcmp(publishYear, lib[i].publishYear))
{
temp[index++] = lib[i].ID;
}
}
sort(temp, temp+index);
return index;
}

int main()
{
unsigned int num, queryNum;
records *lib;
int **result;							// 记录每次询问的结果	result[i][0]用来记录第i次询问的答案个数
char **querys;							// 记录每次询问的问题
int count;
char buffer[120];

cin >> num;
lib = new records[num];
for (int i = 0; i != num; ++i)
{
cin >> lib[i].ID;
cin.get();
cin.getline(lib[i].title, 85);
cin.getline(lib[i].author, 85);
cin.getline(buffer, 120);
lib[i].keyWordNum = printBufferToKeyword(buffer, &lib[i]);
cin.getline(lib[i].publisher, 85);
cin.getline(lib[i].publishYear, 6);
}
cin >> queryNum;
result = new int*[queryNum];
querys = new char*[queryNum];
cin.get();
for (int i = 0; i != queryNum; ++i)
{
querys[i] = new char[80];
cin.getline(querys[i], 120);
switch(querys[i][0])
{
case '1':
count = titleMatch(lib, num, querys[i]+3);
result[i] = new int[count+1];
result[i][0] = count;
memcpy(result[i]+1, temp, count*sizeof(int));
break;
case '2':
count = authorMatch(lib, num, querys[i]+3);
result[i] = new int[count+1];
result[i][0] = count;
memcpy(result[i]+1, temp, count*sizeof(int));
break;
case '3':
count = keyWordMatch(lib, num, querys[i]+3);
result[i] = new int[count+1];
result[i][0] = count;
memcpy(result[i]+1, temp, count*sizeof(int));
break;
case '4':
count = publisherMatch(lib, num, querys[i]+3);
result[i] = new int[count+1];
result[i][0] = count;
memcpy(result[i]+1, temp, count*sizeof(int));
break;
case '5':
count = publishYearMatch(lib, num, querys[i]+3);
result[i] = new int[count+1];
result[i][0] = count;
memcpy(result[i]+1, temp, count*sizeof(int));
break;
}
}

for (int i = 0; i != queryNum; ++i)
{
cout << querys[i] << endl;
if (0 == result[i][0])
{
cout << "Not Found" << endl;
}
else
{
for (int j = 1; j <= result[i][0]; ++j)
{
printf("%07d\n", result[i][j]);
}
}
}

delete[] lib;
for (int i = 0; i != queryNum; ++i)
{
delete[] result[i];
delete[] querys[i];
}
delete[] result;
delete[] querys;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: