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:
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
#include <iostream> #include <map> #include <set> #include <string> using namespace std; map<string,set<int>> book[6]; //分别代表5种映射关系(用set可自动排序) int main(){ int n,id,type; cin>>n; string s; for(int i=0;i<n;i++){ cin>>id; char c=getchar(); //接收掉cin后面的换行 for(int j=1;j<=5;j++){ if(j!=3){ //除了第三行关键词 getline(cin,s); book[j][s].insert(id); } else{ while(cin>>s){ //第三行每次读入一个关键次 book[j][s].insert(id); c=getchar();//接收关键词后的字符 if(c=='\n') break; //若为换行,则结束关键词输入 } } } } cin>>n; for(int i=1;i<=n;i++){ scanf("%d: ",&type); //输入查询类型 getline(cin,s); cout<<type<<": "<<s<<endl; map<string,set<int>>::iterator mp=book[type].find(s); //返回查询到s的迭代器 if(mp==book[type].end()) cout<<"Not Found"<<endl; //若map中没有s else{ for(set<int>::iterator it=book[type][s].begin();it!=book[type][s].end();it++) printf("%07d\n",*it); //输入s对应的所有id } } return 0; }
易错点:输出是没有固定长度(7位),PAT上最后两个测试点没法通过; #include <iostream> #include <cstdio> #include <set> #include <map> #include <string> using namespace std; const int maxn = 10010; int N, M; map<string, set<int>>s_title, s_author, s_key, s_pub, s_year; void query(int num,string inf) { set<int>::iterator it; switch (num) { case 1: if (s_title.find(inf) == s_title.end()) { printf("Not Found\n"); return; } for (it = s_title[inf].begin(); it != s_title[inf].end(); it++) { printf("%07d\n", *it); } break; case 2: if (s_author.find(inf) == s_author.end()) { printf("Not Found\n"); return; } for (it = s_author[inf].begin(); it != s_author[inf].end(); ++it) { printf("%07d\n", *it); } break; case 3: if (s_key.find(inf) == s_key.end()) { printf("Not Found\n"); return; } for (it = s_key[inf].begin(); it != s_key[inf].end(); ++it) { printf("%07d\n", *it); } break; case 4: if (s_pub.find(inf) == s_pub.end()) { printf("Not Found\n"); return; } for (it = s_pub[inf].begin(); it != s_pub[inf].end(); ++it) { printf("%07d\n", *it); } break; case 5: if (s_year.find(inf) == s_year.end()) { printf("Not Found\n"); return; } for (it = s_year[inf].begin(); it != s_year[inf].end(); ++it) { printf("%07d\n", *it); } break; } } int main() { int ID; string title, author, key, pub, year; cin >> N; for (int i = 0; i < N; ++i) { scanf("%d", &ID); getline(cin, title); getline(cin, title); s_title[title].insert(ID); getline(cin, author); s_author[author].insert(ID); while (cin >> key) { s_key[key].insert(ID); char c = getchar(); if (c == '\n')break; } getline(cin, key); s_pub[key].insert(ID); cin >> year; s_year[year].insert(ID); } //开始查询 cin >> M; int num; string cate,inf; for (int i = 0; i < M; ++i) { scanf("%d: ", &num); //num = cate[0] - '0'; getline(cin,inf); cout <<num<<": " << inf<<endl; query(num, inf); } return 0; }
d = {} for i in range(int(input())): b = [input() for j in range(6)] for j in b[1:3] + b[3].split() + b[4:]: try: d[j].append(b[0]) except: d[j] = [b[0]] for i in range(int(input())): b = input() try: print('\n'.join([b] + sorted(d[b[3:]]))) except: print(b + "\nNot Found")
#include<bits/stdc++.h> using namespace std; map<string,set<int> > mpt,mpa,mpk,mpp,mpy; void query(map<string,set<int> > & mp,string & q) { if(mp.find(q)==mp.end()) cout<<"Not Found"<<endl; else { for(set<int>::iterator it=mp[q].begin(); it!=mp[q].end(); it++) { printf("%07d\n",*it); } } } int main() { int n,m,id; string title,author,key,publisher,year; cin>>n; for(int i=0; i<n; i++) { cin>>id; getchar(); getline(cin,title); mpt[title].insert(id); getline(cin,author); mpa[author].insert(id); while(cin>>key) { mpk[key].insert(id); char c=getchar(); if(c=='\n') break; } getline(cin,publisher); mpp[publisher].insert(id); getline(cin,year); mpy[year].insert(id); } cin>>m; int type; string q; for(int i=0; i<m; i++) { scanf("%d: ",&type); getline(cin,q); cout<<type<<": "<<q<<endl; if(type==1) query(mpt,q); else if(type==2) query(mpa,q); else if(type==3) query(mpk,q); else if(type==4) query(mpp,q); else query(mpy,q); } return 0; }
#include<iostream> #include<string> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include<bits/stdc++.h> #define INF 2147483647 #define MIN INF+1 #define ll long long using namespace std; struct book { string info[6]; set<string> keywords; }; bool cmp(book b1, book b2) { return b1.info[0] < b2.info[0]; } book b[10005]; bool check(int i, int lb, int ub) { if(i >= lb && i <= ub) return true; cout << "|" << i << "|" << endl; return false; } int main() { // std::ios::sync_with_stdio(false); // std::cin.tie(0); string Ns, Ms; getline(cin, Ns); int N = atoi(Ns.c_str()); for(int i = 0; i < N; i++) { check(i, 0, N); getline(cin, b[i].info[0]); getline(cin, b[i].info[1]); getline(cin, b[i].info[2]); getline(cin, b[i].info[3]); getline(cin, b[i].info[4]); getline(cin, b[i].info[5]); std::string s = b[i].info[3]; check(i, 0, N); while(s.find(" ") != string::npos) { string key = s.substr(0, s.find(" ")); s = s.substr(s.find(" ") + 1); b[i].keywords.insert(key); } b[i].keywords.insert(s); } // 按照ID升序排序 sort(b, b + N, cmp); getline(cin, Ms); int M = atoi(Ms.c_str()); while(M--) { string query; getline(cin, query); cout << query << endl; int qn = query[0] - '0'; string qs = query.substr(3, -1); bool has = false; for(int i = 0; i < N; i++) { if(qn == 3) { check(i, 0, N); check(qn, 0, 5); if(b[i].keywords.find(qs) != b[i].keywords.end()) { has = true; cout << b[i].info[0] << endl; } } else { check(i, 0, N); check(qn, 0, 5); if(b[i].info[qn] == qs) { cout << b[i].info[0] << endl; has = true; } } } if(!has) { cout << "Not Found" << endl; } } return 0; }
n = input() n = int(n) tem = [[] for i in range(n)] for i in range(n): for j in range(0,6): tem[i].append(input()) tem.sort(key=lambda x:(x[0])) m = input() m = int(m) for i in range(m): boo = True temp= input() print(temp) index = int(temp[0]) temp = temp[3:] for j in tem: if temp == j[index]: print(j[0]) boo = False else: klst = list(j[index].split()) for kt in klst: if temp == kt: print(j[0]) boo = False break if boo: print("Not Found")需要知道的是,开头的数字 代表要寻找的内容范围book title,author,keywords,publisher,year...
思路:构建结构体,然后求解,mb,老子的代码怎么这么长?? #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> #include <algorithm> using namespace std; struct book { string ID; string title; string author; vector<string> keywords; string publisher; string year; }; vector<string> SearchSystem(const vector<struct book> & v, string query) { vector<string> rlt; if (query.size() == 0) { return rlt; } string year; string queryAvil = query.substr(3); switch (query[0]) { case '1'://title for (int i = 0; i < v.size(); i++) { if (queryAvil == v[i].title) rlt.push_back(v[i].ID); } break; case '2'://author for (int i = 0; i < v.size(); i++) { if (queryAvil == v[i].author) rlt.push_back(v[i].ID); } break; case '3'://keywords for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v[i].keywords.size(); j++) { if (queryAvil == v[i].keywords[j]) { rlt.push_back(v[i].ID); } } } break; case '4':// publisher for (int i = 0; i < v.size(); i++) { if (queryAvil == v[i].publisher) rlt.push_back(v[i].ID); } break; case '5':// year for (int i = 0; i < v.size(); i++) { if (queryAvil == v[i].year) rlt.push_back(v[i].ID); } break; default: break; } if (rlt.size() == 0) { rlt.push_back("Not Found"); } return rlt; } bool Cmp(struct book & a, struct book & b) { return a.ID < b.ID; } int main() { int N,M; //ifstream cin("test.txt"); while (cin >> N) { vector<struct book> bookDatabase(N); for (int i = 0; i < N; i++) { cin >> bookDatabase[i].ID; char c = getchar(); //cin.get(); getline(cin, bookDatabase[i].title); getline(cin, bookDatabase[i].author); string tmp; string temp; getline(cin, tmp); for (int j = 0; tmp[j] != '\0'; j++) { if(tmp[j]!=' ') temp += tmp[j]; if (j < tmp.size() && (tmp[j+1] == ' ' || tmp[j+1] == '\0' )) { bookDatabase[i].keywords.push_back(temp); temp = ""; } } getline(cin, bookDatabase[i].publisher); cin >> bookDatabase[i].year; } sort(bookDatabase.begin(), bookDatabase.end(), Cmp); cin >> M; char c = getchar(); //cin.get(); string query; for (int i = 0; i < M; i++) { getline(cin, query); vector<string> rlt = SearchSystem(bookDatabase, query); cout << query << endl; for (int j = 0; j < rlt.size(); j++) { cout << rlt[j] << endl; } } } system("pause"); }
#include <iostream> #include <cstdio> #include <map> #include <set> using namespace std; map<string, set<int> > title, author, key, pub, year; void query(map< string, set<int> > &m, string &str) { if(m.find(str) != m.end()) { for(set<int>::iterator 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", &id); getchar();// getline(cin, ttitle); title[ttitle].insert(id); getline(cin, tauthor); author[tauthor].insert(id); while(cin >> tkey) { key[tkey].insert(id); char c; 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; }
#include <algorithm> #include <string> #include <unordered_map> #include <unordered_set> #include <cstdio> #include <vector> #include <sstream> using namespace std; const int maxn = 500, item = 6, interval = 3; unordered_map <string, unordered_set <string> > titleList; unordered_map <string, unordered_set <string> > authorList; unordered_map <string, unordered_set <string> > keyWordList; unordered_map <string, unordered_set <string> > publisherList; unordered_map <int, unordered_set <string> > publishYearList; vector <string> tempList; int main() { int m, n, i; string str, bookId; scanf("%d", &m); getchar(); while (m--) { for (i = 0; i < item; i++) { char s[maxn]; scanf("%[^\n]", s); getchar(); str = s; if (i == 0) bookId = str; else if (i == 1) titleList[str].insert(bookId); else if (i == 2) authorList[str].insert(bookId); else if (i == 3) { vector <string> keyWordSave; istringstream ss (str); do { string keyWordTemp; ss >> keyWordTemp; keyWordSave.push_back(keyWordTemp); } while (ss); keyWordSave.pop_back(); for (auto &j : keyWordSave) { keyWordList[j].insert(bookId); } } else if (i == 4) publisherList[str].insert(bookId); else publishYearList[stoi(str, nullptr, 16)].insert(bookId); } } scanf("%d", &n); getchar(); while (n--) { char s[maxn]; scanf("%[^\n]", s); getchar(); str = s; string query (str, interval); vector <string> ().swap(tempList); if (str[0] == '1') copy(titleList[query].begin(), titleList[query].end(), back_inserter(tempList)); else if (str[0] == '2') copy(authorList[query].begin(), authorList[query].end(), back_inserter(tempList)); else if (str[0] == '3') copy(keyWordList[query].begin(), keyWordList[query].end(), back_inserter(tempList)); else if (str[0] == '4') copy(publisherList[query].begin(), publisherList[query].end(), back_inserter(tempList)); else { int queryInt = stoi(query, nullptr, 16); copy(publishYearList[queryInt].begin(), publishYearList[queryInt].end(), back_inserter(tempList)); } printf("%s\n", str.c_str()); if (tempList.size() == 0) printf("Not Found\n"); else { sort(tempList.begin(), tempList.end()); for (auto &j : tempList) printf("%s\n", j.c_str()); } } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<map> using namespace std; struct Node{ string ID; }; vector<string> StrSplit(string str,char ch){ vector<string> result; int pos = 0; for (int i=0; i<str.size(); ++i) { pos = (int)str.find(ch,i); if(pos<str.size()){//找到字符串. string te = str.substr(i,pos-i); result.push_back(te); i = pos; }else{ string te = str.substr(i,str.size()-1); result.push_back(te); break; } } return result; } vector<Node>allBooks; bool cmp(int a,int b) { return allBooks[a].ID<allBooks[b].ID; } int main(){ int N,M,kind; scanf("%d%*c",&N); map<string, vector<int>> authorMap,keyWordMap,titleMap,publisherMap,yearMap; string title,author,keystr,publisher,year,query; vector<string>curKeys; allBooks.resize(N); for(int i=0;i<N;++i) { getline(cin,allBooks[i].ID); getline(cin, title); titleMap[title].push_back(i); getline(cin, author); authorMap[author].push_back(i); getline(cin, keystr); vector<string> keys = StrSplit(keystr,' '); for (int j=0; j<keys.size();++j) keyWordMap[keys[j]].push_back(i); getline(cin, publisher); publisherMap[publisher].push_back(i); getline(cin, year); yearMap[year].push_back(i); } scanf("%d",&M); for (int i=0; i<M;++i) { scanf("%d: ",&kind); getline(cin, query); printf("%d: %s\n",kind,query.c_str()); vector<int>res; switch (kind) { case 1: res.assign(titleMap[query].begin(),titleMap[query].end()); break; case 2: res.assign(authorMap[query].begin(),authorMap[query].end()); break; case 3: res.assign(keyWordMap[query].begin(),keyWordMap[query].end()); break; case 4: res.assign(publisherMap[query].begin(),publisherMap[query].end()); break; case 5: res.assign(yearMap[query].begin(),yearMap[query].end()); break; } if (res.size()==0) printf("Not Found\n"); else{ sort(res.begin(),res.end(),cmp); for (int j=0; j<res.size(); ++j) printf("%s\n",allBooks[res[j]].ID.c_str()); } } return 0; }
class Book: def __init__(self, ID, title, author, keyWords, publisher, year): self.ID = ID; self.title = title self.author = author; self.keyWords = keyWords self.publisher = publisher; self.year = year def __cmp__(self, other): return cmp(self.ID, other.ID) N = input() books = [0]*N for i in range(N): ID = raw_input(); title = raw_input() author = raw_input(); keyWords = raw_input().strip().split() publisher = raw_input(); year = raw_input() books[i] = Book(ID, title, author, keyWords, publisher, year) books.sort() K = input() while K: K -= 1 string = raw_input().strip() print string idx, text = string.split(': ') idx = int(idx); rlt = [] if idx == 3: for i in range(N): if text in books[i].keyWords: rlt.append(books[i].ID) elif idx == 1: for i in range(N): if text == books[i].title: rlt.append(books[i].ID) elif idx == 2: for i in range(N): if text == books[i].author: rlt.append(books[i].ID) elif idx == 4: for i in range(N): if text == books[i].publisher: rlt.append(books[i].ID) else: for i in range(N): if text == books[i].year: rlt.append(books[i].ID) if rlt: for i in rlt: print i else: print 'Not Found'