1022 Digital Library (30分) PAT 甲级 我的代码(multimap实现)和别人精简的代码 +意外收获

目录

1022 Digital Library (30分)

Input Specification:

Output Specification:

Sample Input:

Sample Output:

我的代码有点冗长,但是是我智慧的结晶

是时候放出大佬的代码了


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 (≤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

我的代码有点冗长,但是是我智慧的结晶

分类输出的那个地方还可以精简。

#include<iostream>
#include<map>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef multimap<string,int>::iterator itr;
vector<int> temp;
multimap<string,int> ttl,ath,kwd,pbl,year;
void output(int d,string s)
{
	cout<<d<<": "<<s<<endl;
	itr start,end;
	switch(d){
		case 1:{
			if(ttl.count(s)!=0)
			{
				start=ttl.lower_bound(s);
				end=ttl.upper_bound(s);
				temp.clear();
				for(;start!=end;start++)
				{
					temp.push_back(start->second);
				}
				sort(temp.begin(),temp.end());
				for(int i=0;i<temp.size();i++)
				printf("%07d\n",temp[i]);
			}
			else
			cout<<"Not Found"<<endl;
			break;
		}
		case 2:{
			if(ath.count(s)!=0)
			{
				start=ath.lower_bound(s);
				end=ath.upper_bound(s);
				temp.clear();
				for(;start!=end;start++)
				{
					temp.push_back(start->second);
				}
				sort(temp.begin(),temp.end());
				for(int i=0;i<temp.size();i++)
				printf("%07d\n",temp[i]);
			}
			else
			cout<<"Not Found"<<endl;
			break;
		}
		case 3:{
			if(kwd.count(s)!=0)
			{
				start=kwd.lower_bound(s);
				end=kwd.upper_bound(s);
				temp.clear();
				for(;start!=end;start++)
				{
					temp.push_back(start->second);
				}
				sort(temp.begin(),temp.end());
				for(int i=0;i<temp.size();i++)
				printf("%07d\n",temp[i]);
			}
			else
			cout<<"Not Found"<<endl;
			break;
		}
		case 4:{
			if(pbl.count(s)!=0)
			{
				start=pbl.lower_bound(s);
				end=pbl.upper_bound(s);
				temp.clear();
				for(;start!=end;start++)
				{
					temp.push_back(start->second);
				}
				sort(temp.begin(),temp.end());
				for(int i=0;i<temp.size();i++)
				printf("%07d\n",temp[i]);
			}
			else
			cout<<"Not Found"<<endl;
			break;
		}
		case 5:{
			if(year.count(s)!=0)
			{
				start=year.lower_bound(s);
				end=year.upper_bound(s);
				temp.clear();
				for(;start!=end;start++)
				{
					temp.push_back(start->second);
				}
				sort(temp.begin(),temp.end());
				for(int i=0;i<temp.size();i++)
				printf("%07d\n",temp[i]);
			}
			else
			cout<<"Not Found"<<endl;
			break;
		}
	}
}
int main()
{
	int n,i,j,id,t;
	string title,name,kw,pl,yr,s;
	ttl.clear();
	ath.clear();
	kwd.clear();
	pbl.clear();
	year.clear();
	cin>>n;
	for(i=0;i<n;i++)
	{
		scanf("%d\n",&id);
		
//		cout<<id<<endl;
//		cin>>id;

//		cin>>title;
//		cout<<title<<endl;cout<<123<<endl;
		getline(cin,title);
//		cout<<"title   :"<<title<<endl;
		getline(cin,name);
//		cout<<"name   :"<<name<<endl;
		getline(cin,kw);
//		cout<<"kw   :"<<kw<<endl;
		getline(cin,pl);
//		cout<<"pl   :"<<pl<<endl;
		cin>>yr;
//		cout<<"yr    :"<<yr<<endl;
		stringstream ss(kw);
		string str;
		
		ttl.insert(make_pair(title,id));
		ath.insert(make_pair(name,id));
		while(ss>>str)
		{
			kwd.insert(make_pair(str,id));
		}
		pbl.insert(make_pair(pl,id));
		year.insert(make_pair(yr,id));
	}
//	cout<<endl<<endl;
//	
//	for(itr ir=kwd.begin();ir!=kwd.end();ir++)
//	cout<<ir->first<<"  "<<ir->second<<endl;
//	
//	cout<<endl<<endl;
	
	cin>>n;
	for(i=0;i<n;i++)
	{
		scanf("%d:\n",&t);
		getline(cin,s);
		output(t,s);
	}
	
	return 0;
 } 

上面的代码,你可能注意到了scanf里有一个换行符,

下面是详解:  这就是意外收获,成长、进步!

https://blog.csdn.net/qq_41325698/article/details/103437780

是时候放出大佬的代码了

实现思路

    map<string, set<int>> book元数据作为键值, 书号作为值封装输入数据
        其中,set<int> 避免排序问题
        key words 关键词可能存在多个,空格分隔
    循环读入查询类型、查询字符串,调用查询函数打印输出
        函数参数使用引用,否则可能存在超时问题(未验证,最后一组数据)
    输入读取、处理较为耗时

tips

    数据结构封装,便于查询
    set 容器,避免排序

题目难度中等,45分钟内完全可以a题 (咦,我一个小时a的,看来我还是有可能超过大佬的,(#^.^#))
没有对比,就没有伤害!

#include<map>
#include<set>
#include<string>
#include<iostream>
using namespace std;

void query(map<string, set<int>>& mp, string& str)
{
    if(mp.find(str) == mp.end())
        printf("Not Found\n");
    else
    {
        for(set<int>::iterator it=mp[str].begin(); it!=mp[str].end(); it++)
            printf("%07d\n", *it);
    }
}

int main()
{
    int n, m, id, type;
    scanf("%d", &n);
    string title, author, key, pub, year;
    map<string, set<int>> mpTitle, mpAuthor, mpKey, mpPub, mpYear;
    for(int i=0; i<n; i++)
    {
        scanf("%d", &id);
        char c = getchar();
        getline(cin, title);
        mpTitle[title].insert(id);
        getline(cin, author);
        mpAuthor[author].insert(id);
        while(cin >> key)
        {
            mpKey[key].insert(id);
            c = getchar();
            if(c == '\n') break;
        }
        getline(cin, pub);
        mpPub[pub].insert(id);
        getline(cin, year);
        mpYear[year].insert(id);
    }

    string searchKey;
    scanf("%d", &m);
    for(int i=0; i<m; i++)
    {
        scanf("%d: ", &type);
        getline(cin, searchKey);
        cout << type << ": " << searchKey << endl;
        if(type == 1) query(mpTitle, searchKey);
        else if(type==2) query(mpAuthor, searchKey);
        else if(type==3) query(mpKey, searchKey);
        else if(type==4) query(mpPub, searchKey);
        else query(mpYear, searchKey);
    }

    return 0;
}

知识点小结

// 传参引用
void query(map<string, set<int>>& mp, string& str) {}

// 遍历方式
for(set<int>::iterator it=mp[str].begin(); it!=mp[str].end(); it++)

// set<int> 容器查找,字符串是否存在键集合中
if(mp.find(str) == mp.end())


 

发布了374 篇原创文章 · 获赞 101 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/103437354