标签:
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:
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:
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: blablablaSample 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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std;
struct Book
{
int id;
string title = "";
string author = "";;
string keywords[5];
int keywordCnt = 0;
string publisher = "";;
string publishYear;
};
vector<Book> b;
bool cmp1(Book a, Book c) {
return a.id < c.id;
}
int main(void) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char ctemp;
Book btemp;
cin >> btemp.id;
scanf("%c", &ctemp);
scanf("%c", &ctemp);
while (ctemp!=‘\n‘)
{
btemp.title += ctemp;
scanf("%c", &ctemp);
}
//getline(cin, btemp.title);
scanf("%c", &ctemp);
while (ctemp != ‘\n‘)
{
btemp.author += ctemp;
scanf("%c", &ctemp);
}
int keyCnt = 0,keyCntFlag=0;
scanf("%c", &ctemp);
while (ctemp != ‘\n‘)
{
if (ctemp != ‘ ‘) {
if (keyCntFlag == 1) {
keyCnt++;
keyCntFlag = 0;
}
btemp.keywords[keyCnt] += ctemp;
}
else {
keyCntFlag = 1;
}
if (keyCnt > 4)
break;
scanf("%c", &ctemp);
}
btemp.keywordCnt = keyCnt+2;
scanf("%c", &ctemp);
while (ctemp != ‘\n‘)
{
btemp.publisher += ctemp;
scanf("%c", &ctemp);
}
// scanf("%d", &btemp.publishYear);
scanf("%c", &ctemp);
while (ctemp != ‘\n‘)
{
btemp.publishYear += ctemp;
scanf("%c", &ctemp);
}
b.push_back(btemp);
}
sort(b.begin(), b.end(),cmp1);
int m;
cin >> m;
char ctemp;
scanf("%c", &ctemp);
for (int i = 0; i < m; i++) {
string search = "";
scanf("%c", &ctemp);
while (ctemp != ‘\n‘)
{
search += ctemp;
scanf("%c", &ctemp);
}
cout << search << endl;
int searchType = search[0]-‘0‘;
string searchTemp = search;
search = "";
for (int j = 0; j < searchTemp.length() - 3; j++)
search+= searchTemp[j + 3];
int outCnt = 0;
if (searchType == 1) {
for (int q = 0; q < n; q++) {
if (search == b[q].title) {
// cout << b[q].id << endl;
printf("%07d\n", b[q].id);
outCnt++;
}
}
if (outCnt == 0)
printf("Not Found\n");
}
else if (searchType == 2) {
for (int q = 0; q < n; q++) {
if (search == b[q].author) {
printf("%07d\n", b[q].id); outCnt++;
}
}
if (outCnt == 0)
printf("Not Found\n");
}
else if (searchType == 3) {
for (int q = 0; q < n; q++) {
for (int p = 0; p < 5; p++) {
if (search == b[q].keywords[p]&&search!="") {
printf("%07d\n", b[q].id); outCnt++;
}
}
}
if (outCnt == 0)
printf("Not Found\n");
}
else if (searchType == 4) {
for (int q = 0; q < n; q++) {
if (search == b[q].publisher) {
printf("%07d\n", b[q].id); outCnt++;
}
}
if (outCnt == 0)
printf("Not Found\n");
}
else if (searchType == 5) {
for (int q = 0; q < n; q++) {
if (search == b[q].publishYear) {
printf("%07d\n", b[q].id); outCnt++;
}
}
if (outCnt == 0)
printf("Not Found\n");
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5023081.html