标签:
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:
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:
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 <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #include <string.h> 5 6 #include <math.h> 7 #include <algorithm> 8 #include <vector> 9 #include <set> 10 #include <string> 11 #include <map> 12 using namespace std; 13 map<string,set<int> >mptitle,mpauthor,mpkey,mppub,mpyear; 14 //一个关键字可以对应多个id,所以使用set<int> 15 void query(map<string,set<int> >&mp,string &str) 16 { 17 if(mp.find(str)==mp.end())printf("Not Found\n"); 18 else{ 19 for(set<int>::iterator it=mp[str].begin();it!=mp[str].end();it++)//对于已找到的关键字 全部输出 20 { 21 //注意这里使用的是 set<int>,而不是map<string,set<int> 22 printf("%07d\n",*it); 23 24 } 25 } 26 } 27 28 29 int main(){ 30 int n,id; 31 string title,author,key,pub,year; 32 scanf("%d",&n); 33 for(int i=0;i<n;i++) 34 { 35 scanf("%d",&id); 36 char c=getchar();//获取换行 37 getline(cin,title); 38 mptitle[title].insert(id); 39 getline(cin,author); 40 mpauthor[author].insert(id); 41 while(cin>>key) 42 { 43 mpkey[key].insert(id); 44 c=getchar(); 45 if(c==‘\n‘)break; 46 } 47 getline(cin,pub); 48 mppub[pub].insert(id); 49 getline(cin,year); 50 mpyear[year].insert(id); 51 } 52 string temp; 53 int m,type; 54 scanf("%d",&m); 55 for(int i=0;i<m;i++) 56 { 57 scanf("%d: ",&type);//这里格式很重要 空格冒号不能少 58 getline(cin,temp); 59 cout<<type<<": "<<temp<<endl; 60 if(type==1)query(mptitle,temp); 61 else if(type==2)query(mpauthor,temp); 62 else if(type==3)query(mpkey,temp); 63 else if(type==4)query(mppub,temp); 64 else query(mpyear,temp); 65 } 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/ligen/p/4309839.html