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

    1. 题目

    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 (≤10^4) 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 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
    

    2. 题意

    虽然题目描述一大堆,但是题意比较简单,就是书籍信息有6个字段,其中书籍ID号是唯一的,我们需要做的是存储好书籍信息和对应的ID号;然后要做搜索书籍ID号的操作,分别根据除ID号外的其他五个字段来搜索相应的ID号列表。

    3. 思路——map+set

    1. 显然这里使用到map来存储相应的字段ID号列表键值对信息,分别开五个map来分别存储不同字段。注:观察输出信息,可以看到ID号输出是有序的,且不重复的,所以ID号列表信息是用set存储刚好合适。
    2. 存储好后,搜索时,根据要搜索的字段,调出相应的map,并根据搜索信息去map中查找对应的键值,如果找到,则输出对应的ID号列表。
    3. 整体思路就是12两个步骤了,接下来说说这题做的过程中需要注意的一些细节:
      • 首先是书籍信息和搜索信息的输入,需要整行输入,如果直接cin输入,则读到空格就会结束,这里使用到getline(cin, str)(头文件为<string>)来进行输入。使用注意:如果在使用getline(cin,str)之前为cin输入等,往往需要使用getchar()去吃掉换行。
      • printf输出string时会有错误的问题,需要使用str.c_str()函数将string转化为字符数组形式,即可输出。
      • auto关键字很好用!

    4. 代码

    #include <iostream>
    #include <map>
    #include <set>
    #include <string>
    
    using namespace std;
    
    typedef struct book
    {
    	int num;		// 书籍ID号 
    	string title;		// 书名 
    	string author;		// 作者 
    	string words;		// 书籍关键字 
    	string publisher;	// 出版社 
    	int year;		// 出版年份 
    } Book;
    
    map<string, set<int>> res1;
    map<string, set<int>> res2;
    map<string, set<int>> res3;
    map<string, set<int>> res4;
    map<int, set<int>> res5; 
    
    int num, year;
    string title, author, words, publisher;
    
    void print()	// 测试使用 
    {
    	cout << num << endl;
    	cout << title << endl;
    	cout << author << endl;
    	cout << words << endl;
    	cout << publisher << endl;
    	cout << year << endl;
    }
    
    // 将关键字和书的ID号绑定 
    void saveToRes3()
    {
    	string str = "";
    	for (int i = 0; i < words.length(); ++i)
    	{
    		if (words[i] != ' ') str += words[i];
    		else 
    		{
    			res3[str].insert(num);
    			str = "";
    		} 
    	}
    	res3[str].insert(num);
    }
    
    int main()
    {
    	int n;
    	cin >> n;
    	Book books[n];
    	while (n--)	// 输入书籍信息,并将相应信息和书的ID号绑定 
    	{
    		cin >> num;
    		getchar();	// 读取整行前,需要将前面的换行符吃掉 
    		getline(cin, title);
    		getline(cin, author);
    		getline(cin, words);
    		getline(cin, publisher);
    		cin >> year;
    //		print();		
    		res1[title].insert(num);
    		res2[author].insert(num);
    		saveToRes3();
    		res4[publisher].insert(num);
    		res5[year].insert(num);
    	}
    	int m;
    	cin >> m;
    	string str;
    	getchar();	// 读取整行前,需要将前面的换行符吃掉 
    	while (m--)
    	{
    		getline(cin, str);
    		printf("%s\n", str.c_str());
    		char select = str[0];
    		string key = str.substr(3, str.size());
    		// 根据搜索要求,输出相应书的ID号 
    		switch (select)
    		{
    			case '1':
    				for (auto it = res1[key].begin(); it != res1[key].end(); it++)
    					printf("%07d\n", *it);
    				if (res1[key].size() == 0) printf("Not Found\n");
    				break;
    			case '2':
    				for (auto it = res2[key].begin(); it != res2[key].end(); it++)
    					printf("%07d\n", *it);
    				if (res2[key].size() == 0) printf("Not Found\n");
    				break;
    			case '3':
    				for (auto it = res3[key].begin(); it != res3[key].end(); it++)
    					printf("%07d\n", *it);
    				if (res3[key].size() == 0) printf("Not Found\n");				
    				break;
    			case '4':
    				for (auto it = res4[key].begin(); it != res4[key].end(); it++)
    					printf("%07d\n", *it);
    				if (res4[key].size() == 0) printf("Not Found\n");				
    				break;
    			case '5':
    				for (auto it = res5[atoi(key.c_str())].begin(); it != res5[atoi(key.c_str())].end(); it++)
    					printf("%07d\n", *it);
    				if (res5[atoi(key.c_str())].size() == 0) printf("Not Found\n");				
    				break;		
    		}
    	}
    } 
    
  • 相关阅读:
    R语言--自定义函数证明中心极限定理
    R基础for、while、自定义函数
    R基础绘图、时间序列生成
    装R包(package)error:(as ‘lib’ is unspecified)installation of package had non-zero exit status
    GraphPad Prism中文显示错误
    SAS学笔记_chapter_library
    excel表格打印时铺满纸张
    SAS学笔记_chapter1_基本概念
    支持向量机课(SVM)(五)--SMO算法(省去公式推导,只讲其思想)
    Web前端学习-第六课HTML篇
  • 原文地址:https://www.cnblogs.com/vanishzeng/p/15479920.html
Copyright © 2011-2022 走看看