PAT 1022. Digital Library (30)

1022. Digital Library (30)

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

采用map来进行查找,题目中所说的year在[1000, 3000]之间,但是所给的测试并不一定在这之间,否则会测试点2不能通过。为了简单的话可以变量都设成数组,以下代码为分开写,略微繁琐
  1 #include <iostream>
  2 #include <set>
  3 #include <string>
  4 #include <map>
  5 #include <sstream>
  6 #include <algorithm>
  7 
  8 using namespace std;
  9 
 10 set<string> title[10000];
 11 set<string> author[10000];
 12 set<string> keyword[1001];
 13 set<string> publisher[1001];
 14 set<string> year[2001];
 15 
 16 int main()
 17 {
 18     int recordNum;
 19     cin >> recordNum;
 20     getchar();
 21     int titleLast, authorLast, keywordLast, publisherLast, yearLast;
 22     titleLast = authorLast = keywordLast = publisherLast = yearLast = 0;
 23     map<string, int> titleIndex, authorIndex, keywordIndex, publisherIndex;
 24     for (int i = 0; i < recordNum; i++)
 25     {
 26         int y;
 27         string id, t, a, k, p;
 28         getline(cin, id);
 29         getline(cin, t);
 30         getline(cin, a);
 31         getline(cin, k);
 32         getline(cin, p);
 33         cin >> y;
 34         getchar();
 35         //title
 36         if (titleIndex.find(t) == titleIndex.end())
 37         {
 38             title[titleLast].insert(id);
 39             titleIndex[t] = titleLast++;
 40         }
 41         else
 42         {
 43             int index = titleIndex[t];
 44             title[index].insert(id);
 45         }
 46         //author
 47         if (authorIndex.find(a) == authorIndex.end())
 48         {
 49             author[authorLast].insert(id);
 50             authorIndex[a] = authorLast++;
 51         }
 52         else
 53         {
 54             int index = authorIndex[a];
 55             author[index].insert(id);
 56         }
 57         //keyword
 58         string key;
 59         stringstream ss;
 60         ss << k;
 61         while (ss >> key)
 62         {
 63             if (keywordIndex.find(key) == keywordIndex.end())
 64             {
 65                 keyword[keywordLast].insert(id);
 66                 keywordIndex[key] = keywordLast++;
 67             }
 68             else
 69             {
 70                 int index = keywordIndex[key];
 71                 keyword[index].insert(id);
 72             }
 73         }
 74         //publisher
 75         if (publisherIndex.find(p) == publisherIndex.end())
 76         {
 77             publisher[publisherLast].insert(id);
 78             publisherIndex[p] = publisherLast++;
 79         }
 80         else
 81         {
 82             int index = publisherIndex[p];
 83             publisher[index].insert(id);
 84         }
 85         //year
 86         year[y - 1000].insert(id);
 87     }
 88 
 89     int queryNum;
 90     string query;
 91     cin >> queryNum;
 92     getchar();
 93     for (int i = 0; i < queryNum; i++)
 94     {
 95         getline(cin, query);
 96         string key(query.begin() + 3, query.end());
 97         cout << query << endl;
 98         if (query[0] == '1')
 99         {
100             if (titleIndex.find(key) == titleIndex.end())
101                 cout << "Not Found" << endl;
102             else
103             {
104                 int index = titleIndex[key];
105                 for (set<string>::iterator it = title[index].begin(); it != title[index].end();it++)
106                     cout << *it << endl;
107             }
108         }
109         else if (query[0] == '2')
110         {
111             if (authorIndex.find(key) == authorIndex.end())
112                 cout << "Not Found" << endl;
113             else
114             {
115                 int index = authorIndex[key];
116                 for (set<string>::iterator it = author[index].begin(); it != author[index].end(); it++)
117                     cout << *it << endl;
118             }
119         }
120         else if (query[0] == '3')
121         {
122             if (keywordIndex.find(key) == keywordIndex.end())
123                 cout << "Not Found" << endl;
124             else
125             {
126                 int index = keywordIndex[key];
127                 for (set<string>::iterator it = keyword[index].begin(); it != keyword[index].end(); it++)
128                     cout << *it << endl;
129             }
130         }
131         else if (query[0] == '4')
132         {
133             if (publisherIndex.find(key) == publisherIndex.end())
134                 cout << "Not Found" << endl;
135             else
136             {
137                 int index = publisherIndex[key];
138                 for (set<string>::iterator it = publisher[index].begin(); it != publisher[index].end(); it++)
139                     cout << *it << endl;
140             }
141         }
142         else
143         {
144             stringstream ss;
145             int y;
146             ss << key;
147             ss >> y;
148             if (y<1000 || y>3000 || year[y - 1000].size() == 0)
149                 cout << "Not Found" << endl;    
150             else
151             {
152                 for (set<string>::iterator it = year[y - 1000].begin(); it != year[y - 1000].end(); it++)
153                     cout << *it << endl;
154             }
155         }
156     }    
157 }

 

posted @ 2015-08-24 10:42  JackWang822  阅读(562)  评论(0编辑  收藏  举报