1022 Digital Library
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.
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:
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:
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.
- 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
- 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
总结:用结构体只是写出了题目样例,还是有待进步!
有几个需要注意的点:
cin输入后在缓冲区的还存留着回车,如果此时是使用了getline(cin,s),会读取一个回车,导致结果不正确,需要使用一个getchar(),读取回车
- #include
- #include
- #include
- using namespace std;
-
- struct Books{
- int id;
- string name,author;
- map
int> m; - string publisher,year;
- };
-
- int main(){
- int n,k,t=0;
- cin >> n;
- Books book[n+1];
-
- for(int i=0;i
- cin >> book[i].id;
- getchar();
- getline(cin,book[i].name);
- getline(cin,book[i].author);
- char c;
- string s;
- while(1){
- cin >> s;
- book[i].m[s]=1;
- scanf("%c",&c);
- if(c=='\n') break;
- }
- getline(cin,book[i].publisher);
- getline(cin,book[i].year);
- }
-
- scanf("%d",&k);
- getchar();
- string s,w;
- for(int i=0;i
- getline(cin,s);
- t=s[0]-'0';
- set<int> v;
- if(t>=1 && t<=5) cout << s << endl;
- w=s.substr(3);
- if(t==1){
- for(int j=0;j
- if(book[j].name==w){
- cout << book[j].id << endl;
- }
- }
- }
- else if(t==3){
- for(int j=0;j
- if(book[j].m[w]) v.insert(book[j].id);
- }
- if(!v.size()) printf("Not Found\n");
- else for(auto it=v.begin();it!=v.end();it++) printf("%d\n",*it);
- }
- else if(t==2){
- for(int j=0;j
- if(book[j].author==w) cout << book[j].id << endl;
- }
- }
- else if(t==4){
- for(int j=0;j
- if(book[j].publisher==w) cout << book[j].id << endl;
- }
- }
- else if(t==5){
- for(int j=0;j
- if(book[j].year==w) cout << book[j].id << endl;
- }
- }
- }
-
- return 0;
- }
还是看看到老的代码:
- #include
- #include
- #include
- using namespace std;
- map
int> > title, author, key, pub, year; - void query(map
int > > &m, string &str) { - if(m.find(str) != m.end()) {
- for(auto it = m[str].begin(); it != m[str].end(); it++)
- printf("%07d\n", *it);
- } else
- cout << "Not Found\n";
- }
- int main() {
- int n, m, id, num;
- scanf("%d", &n);
- string ttitle, tauthor, tkey, tpub, tyear;
- for(int i = 0; i < n; i++) {
- scanf("%d\n", &id);
- getline(cin, ttitle);
- title[ttitle].insert(id);
- getline(cin, tauthor);
- author[tauthor].insert(id);
- while(cin >> tkey) {
- key[tkey].insert(id);
- char c = getchar();
- if(c == '\n') break;
- }
- getline(cin, tpub);
- pub[tpub].insert(id);
- getline(cin, tyear);
- year[tyear].insert(id);
- }
- scanf("%d", &m);
- for(int i = 0; i < m; i++) {
- scanf("%d: ", &num);
- string temp;
- getline(cin, temp);
- cout << num << ": " << temp << "\n";
- if(num == 1) query(title, temp);
- else if(num == 2) query(author, temp);
- else if(num == 3) query(key, temp);
- else if(num == 4) query(pub,temp);
- else if(num ==5) query(year, temp);
- }
- return 0;
- }
还是看看大佬的代码:
果然,大佬就是大佬,思路简单易懂
基本思路:将同一类的字符串的id都存在同一个string类型的map中,最后查找看是该字符串是否存在,存在则打印,不存在就打印Not Found
- #include
- #include
- #include
- using namespace std;
- map
int> > title, author, key, pub, year; - //此时用map
> 这样的变量,可以再同一个字符串存储多个id,也就是只要有这个字符串的id都是可以存进去的 - void query(map
int > > &m, string &str) { - if(m.find(str) != m.end()) {
- for(auto it = m[str].begin(); it != m[str].end(); it++)
- printf("%07d\n", *it);
- } else
- cout << "Not Found\n";
- }
- int main() {
- int n, m, id, num;
- scanf("%d", &n);
- string ttitle, tauthor, tkey, tpub, tyear;
- for(int i = 0; i < n; i++) {
- scanf("%d\n", &id);
- getline(cin, ttitle);
- title[ttitle].insert(id);//注意这里的insert插入进去的是set,而不是map
- getline(cin, tauthor);
- author[tauthor].insert(id);
- while(cin >> tkey) {
- key[tkey].insert(id);
- char c = getchar();
- if(c == '\n') break;
- }
- getline(cin, tpub);
- pub[tpub].insert(id);
- getline(cin, tyear);
- year[tyear].insert(id);
- }
- scanf("%d", &m);
- for(int i = 0; i < m; i++) {
- scanf("%d: ", &num);
- string temp;
- getline(cin, temp);
- cout << num << ": " << temp << "\n";
- if(num == 1) query(title, temp);
- else if(num == 2) query(author, temp);
- else if(num == 3) query(key, temp);
- else if(num == 4) query(pub,temp);
- else if(num ==5) query(year, temp);
- }
- return 0;
- }
好好学习,天天向上!
我要考研!
-
相关阅读:
vue3组件封装系列-表格及分页
LeetCode 290. 单词规律
MySQL密码不要用0开头!!!
LAXCUS分布式操作系统是什么?
轻松上手Jackjson(珍藏版)
国庆放假作业5
gRpc_go_dart-1.编写第一个服务
C语言之for while语句详解
高效!启科量子线路模拟器 QuSprout 与 Amazon HPC 集成,赋能量子计算
LSM Tree
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126823286