1022. Digital Library

1022. Digital Library (30)

时间限制
1000 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

  1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <string>
5 #include <algorithm>
6 #include <map>
7 #include <stack>
8 #include <cmath>
9 #include <queue>
10 #include <set>
11 #include <list>
12 #include <stdlib.h>
13 #include <string.h>
14
15
16 using namespace std;
17
18 class Book
19 {
20 public:
21 char id[8];
22 char title[81];
23 char author[81];
24 vector<string> keywords;
25 char publisher[81];
26 char year[5];
27
28
29 };
30
31 bool myComp ( const char* s1 , const char* s2 )
32 {
33 if( s1 == NULL || s2 == NULL )
34 {
35 return true;
36 }
37 int result = strcmp(s1,s2);
38 if( result > 0 )
39 {
40 return false;
41 }
42 else
43 {
44 return true;
45 }
46
47 }
48
49
50
51 int main()
52 {
53
54
55
56 list<Book> lib;
57
58
59 int n;
60
61 cin >> n;
62
63 for( int i = 0 ; i < n ; ++i )
64 {
65 Book book;
66 cin >> book.id;
67
68 cin.get();
69
70
71 cin.getline(book.title , 80);
72
73 cin.getline(book.author , 80);
74
75
76 while( 1 )
77 {
78 string keyword;
79 char ch;
80 while( 1 )
81 {
82 ch = cin.get();
83
84 if( ch == ' ' )
85 {
86 book.keywords.push_back(keyword);
87 break;
88 }
89 else if ( ch == '\n' )
90 {
91 book.keywords.push_back(keyword);
92 goto NEXT_ITEM;
93 }
94 keyword.push_back(ch);
95 }
96
97
98 }
99
100 NEXT_ITEM:;
101
102
103 cin.getline(book.publisher , 80);
104
105 cin.getline(book.year , 80);
106
107
108 lib.push_back( book );
109 }
110
111 cin >> n;
112 cin.get();
113 for( int i = 0 ; i < n ; ++i )
114 {
115 char qry[101];
116 char cmd[101];
117
118
119 cin.getline(qry , 100);
120
121 string qury = qry;
122
123 strcpy ( cmd , qury.substr( 3 , qury.size() - 3 ).c_str());
124
125 char select = qury[0];
126
127 vector<char*> result;
128 result.clear();
129
130 switch(select)
131 {
132 case '1':
133 for( list<Book>::iterator it = lib.begin() ; it != lib.end() ; ++it )
134 {
135 if( strcmp( it->title, cmd ) == 0 )
136 {
137 result.push_back( it->id );
138 }
139 }
140 break;
141 case '2':
142 for( list<Book>::iterator it = lib.begin() ; it != lib.end() ; ++it )
143 {
144 if( strcmp( it->author, cmd) == 0 )
145 {
146 result.push_back( it->id );
147 }
148 }
149 break;
150 case '3':
151 for( list<Book>::iterator it = lib.begin() ; it != lib.end() ; ++it )
152 {
153 for( int kw = 0 ; kw < it->keywords.size() ; ++kw )
154 {
155 if( strcmp ( it->keywords[kw].c_str(), cmd ) == 0 )
156 {
157 result.push_back( it->id );
158 break;
159 }
160 }
161 }
162 break;
163 case '4':
164 for( list<Book>::iterator it = lib.begin() ; it != lib.end() ; ++it )
165 {
166 if( strcmp ( it->publisher, cmd) == 0 )
167 {
168 result.push_back( it->id );
169 }
170 }
171 break;
172 case '5':
173 for( list<Book>::iterator it = lib.begin() ; it != lib.end() ; ++it )
174 {
175 if( strcmp( it->year, cmd) == 0 )
176 {
177 result.push_back( it->id );
178 }
179 }
180 break;
181 }
182
183 sort( result.begin() , result.end() , myComp );
184
185 cout << qury << endl;
186 if( result.size() != 0 )
187 {
188 for ( int r = 0 ; r < result.size() ; ++r )
189 {
190 cout << result[r] << endl;
191 }
192 }
193 else
194 {
195 cout << "Not Found" << endl;
196 }
197
198 }
199
200 return 0;
201 }
There is one case failed with "out of memory". To be debugged...
P.S.
This link: http://blog.csdn.net/tiantangrenjian/article/details/6886093
gives a piece of code that failed wit "time-out" but success in my failed case. It may be funny to combine these two and add a ramdomized selection.

原文地址:https://www.cnblogs.com/kking/p/2331805.html