zoukankan      html  css  js  c++  java
  • 1022. Digital Library (30)

    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:

    • 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

    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 Output1: 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

    题目大意:图书搜索,根据给定的title、author、publisher、year这些索引词来查找书。
    题目涉及的主要是一个排序,然后就是顺序查找,不难。麻烦的地方在于,输入的数据组织。利用struct来存书,每本书中的keywords有多个,所以利用
    vector来存。很久没碰到gets(),都快忘记它的便利之处,即可以输入带空格的字符串。
    #include<iostream>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    struct Book{
    	char id[8];
    	char title[81];
    	char author[81];
    	vector<string>keywords;
    	char publisher[81];
    	char year[5];
    };
    Book book[10002];
    int n;
    bool cmp(Book b1,Book b2){
    	return strcmp(b1.id,b2.id)<0;
    }
    int main(){
    	scanf("%d",&n);
    	int i,j,k;
    	getchar();
    	for(i=0;i<n;i++){
    		gets(book[i].id);
    		gets(book[i].title);
    		gets(book[i].author);
    		do{
    			char tmp[12];
    			scanf("%s",tmp);
    			book[i].keywords.push_back(tmp);
    		}while(getchar()!='
    ');
    		gets(book[i].publisher);
    		gets(book[i].year);
    	}
    	sort(book,book+n,cmp);
    	int m;
    	scanf("%d",&m);
    	int flag = 0;
    	int index = 0;
    	for(i=0;i<m;i++){
    		scanf("%d: ",&k);
    		char ss[82];
    		gets(ss);
    		printf("%d: %s
    ",k,ss);
    		for(j=0;j<n;j++){
    			if(strcmp(book[j].title,ss)==0){
    				flag = 1;
    			}
    			else if(strcmp(book[j].author,ss)==0){
    				flag = 1;
    			}
    			else if(strcmp(book[j].publisher,ss)==0){
    				flag = 1;
    			}
    			else if(strcmp(book[j].year,ss)==0){
    				flag = 1;
    			}
    			else{
    				int length = book[j].keywords.size();
    				for(int t=0;t<length;t++){
    					if(strcmp(book[j].keywords[t].c_str(),ss)==0){
    						flag = 1;
    						break;
    					}
    				}
    			}
    			if(flag == 1){
    				printf("%s
    ",book[j].id);
    				index = 1;
    				flag = 0;
    			}
    		}
    		if(index == 0){
    			printf("Not Found
    ");
    		}
    		index = 0;
    	}
    	return 0;
    }
    

      



  • 相关阅读:
    【Spring学习】Spring的源码解析之路 ——【step1】基础环境配置 + IDEA快捷键整理
    【Spring学习】Spring的源码解析之路
    【Android端】【日志收集上报SDK相关内容测试的方案梳理总结】
    【codelife 阿里技术文章分享——读后感】
    【python深入】map/reduce/lambda 内置函数的使用
    【python深入】单例模式
    【python深入】装饰器理解及使用
    【python深入】collections-Counter使用总结
    【python深入】dict和list实现排序:sorted()和lambda的使用
    选择排序
  • 原文地址:https://www.cnblogs.com/grglym/p/7670623.html
Copyright © 2011-2022 走看看