[编程题]Digital Library (30)
- 热度指数:548时间限制:2秒空间限制:655360K
- 算法知识视频讲解
A Digital Library contains millions of books, stored according to theirtitles, authors, key words of their abstracts, publishers, and publishedyears. Each book is assigned an unique 7-digit number as its ID. Givenany 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 (<=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
输出描述:
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.
示例1
输入
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
// pat.cpp : 定义控制台应用程序的入口点。 //#include "stdafx.h" #include"stdio.h" #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<vector> #include<map> #include<math.h> using namespace std; const int maxn=10010; struct node{ int id; string title; string author; vector<string>keywords; string publisher; string time; node(){} }no[10010];//如果结构体数组开得太大,将会导致程序一开始就没法运行,也不报错 map<string,vector<int>>titles; map<string,vector<int>>authors; map<string,vector<int>>kws; map<string,vector<int>>ps; map<string,vector<int>>ts; int num; string title,author,keyword,publisher,time; int n; int main(){ // freopen("c://jin.txt","r",stdin); cin>>n; for(int i=0;i<n;i++){ cin>>no[i].id; getchar();//在getline前面要加一个getchar()为了吸收到前面的回车符,避免输入空串 getline(cin,no[i].title); //cout<<no[i].title<<endl; getline(cin,no[i].author); //cout<<no[i].author<<endl; char c; while( cin>>keyword){//输入以空格分开的字符串,遇到回车结束 //cout<<keyword<<endl; no[i].keywords.push_back(keyword); kws[keyword].push_back(i); c=getchar();//应该先输入字符串,再getchar看看是不是为回车符 if(c=='\n')break; } getline(cin,no[i].publisher); getline(cin,no[i].time); authors[no[i].author].push_back(i); titles[no[i].title].push_back(i); //cout<<no[i].title<<endl; ps[no[i].publisher].push_back(i); ts[no[i].time].push_back(i); } string s;int x; cin>>n; while(n--){scanf("%d: ",&x); getline(cin,s); vector<int>vec; switch(x){ case 1:{vec=titles[s];break;}//case里面一定要break,否则会出错 case 2:{vec=authors[s];break;} case 3:{vec=kws[s];break;} case 4:{vec=ps[s];break;} case 5:{vec=ts[s];break;} } cout<<x<<": "<<s<<endl; if(vec.size()==0)cout<<"Not Found"<<endl; else { sort(vec.begin(),vec.end()); for(int i=0;i<vec.size();i++){ printf("%07d",no[vec[i]].id); cout<<endl; } } } // freopen("CON","r",stdin); // system("pause"); return 0; }