标签:form sort debug store hit cas another switch col
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 (≤) which is the total number of books. Then Nblocks 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 (≤) 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
第一种方法
以时间换空间,就是个ID保留一组信息,查询时遍历查询
但可能导致时间复杂度过大
第二种方法[推荐使用方法二]
以空间换时间,每种信息对应一个ID,查找时,时间复杂度为O(1)
但可能导致空间复杂度太大
注意一些字符输入的细节
1 #include <iostream> 2 #include <map> 3 #include <unordered_map> 4 #include <set> 5 #include <string> 6 using namespace std; 7 8 方法一: 9 struct node 10 { 11 string name, author, keywords, publisher, year; 12 }; 13 int main() 14 { 15 int N, M; 16 string ID; 17 cin >> N; 18 map<string, node>data; 19 for (int i = 0; i < N; ++i) 20 { 21 node book; 22 cin >> ID; 23 getchar();//去除回车键 24 getline(cin, book.name); 25 getline(cin, book.author); 26 getline(cin, book.keywords); 27 getline(cin, book.publisher); 28 cin >> book.year; 29 data[ID] = book; 30 } 31 cin >> M; 32 getchar();//去除回车键 33 for (int i = 0; i < M; ++i) 34 { 35 string str; 36 bool is = true; 37 getline(cin, str); 38 cout << str << endl; 39 int index = str[0] - ‘0‘; 40 str.assign(str.begin() + 3, str.end()); 41 for (auto ptr = data.begin(); ptr != data.end(); ++ptr) 42 { 43 switch (index) 44 { 45 case 1: 46 if (ptr->second.name == str) 47 { 48 is = false; 49 cout << ptr->first << endl; 50 } 51 break; 52 case 2: 53 if (ptr->second.author== str) 54 { 55 is = false; 56 cout << ptr->first << endl; 57 } 58 break; 59 case 3: 60 if (ptr->second.keywords.find(str) !=-1) 61 { 62 is = false; 63 cout << ptr->first << endl; 64 } 65 break; 66 case 4: 67 if (ptr->second.publisher == str) 68 { 69 is = false; 70 cout << ptr->first << endl; 71 } 72 break; 73 case 5: 74 if (ptr->second.year == str) 75 { 76 is = false; 77 cout << ptr->first << endl; 78 } 79 break; 80 default: 81 break; 82 } 83 } 84 if (is) 85 cout << "Not Found" << endl; 86 } 87 return 0; 88 } 89 90 //方法二 91 void findInfo(unordered_map<string, set<int>>&data,string &str)//传参一定要用引用,否则最后一组数据可能会超时 92 { 93 if(data.find(str)==data.end()) 94 printf("Not Found\n"); 95 else 96 { 97 for (auto ptr = data.find(str)->second.begin(); ptr != data.find(str)->second.end(); ++ptr) 98 printf("%07d\n", *ptr); 99 } 100 } 101 int main() 102 { 103 int N, M, ID; 104 scanf("%d", &N); 105 string til, aut, keys, pub, yea; 106 unordered_map<string, set<int>>title, author, keywords, publisher, year;//因为key不唯一 107 for (int i = 0; i < N; ++i) 108 { 109 scanf("%d\n", &ID);//不用清除回车键 110 getline(cin, til); 111 title[til].insert(ID); 112 getline(cin, aut); 113 author[aut].insert(ID); 114 while (cin >> keys) 115 { 116 keywords[keys].insert(ID); 117 char c = getchar(); 118 if (c == ‘\n‘)break; 119 } 120 getline(cin, pub); 121 publisher[pub].insert(ID); 122 getline(cin, yea); 123 year[yea].insert(ID); 124 } 125 scanf("%d\n", &M); 126 for (int i = 0; i < M; ++i) 127 { 128 string str; 129 getline(cin, str); 130 cout << str << endl; 131 int index = str[0] - ‘0‘; 132 str.assign(str.begin() + 3, str.end()); 133 if (index == 1) findInfo(title, str); 134 else if (index == 2) findInfo(author, str); 135 else if (index == 3) findInfo(keywords, str); 136 else if (index == 4) findInfo(publisher, str); 137 else if (index == 5) findInfo(year, str); 138 else printf("Not Found\n"); 139 } 140 return 0; 141 }
标签:form sort debug store hit cas another switch col
原文地址:https://www.cnblogs.com/zzw1024/p/11247296.html