华为闯关-书签检索

本文介绍了一个开放的书名检索库的设计与实现方法。该库能够根据用户提供的一个或多个关键字进行精确匹配搜索,并返回符合搜索条件的书名。文章详细解释了书名和搜索条件的格式规范,同时提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述: 实现一个开放的书名检索库。库中存储了若干个书名。用户可以:指定搜索条件,
搜索库中符合条件的书名
重要格式说明
单词

由小写英文字母组成,不含其它字符。
书名
由一个或多个单词组成。
当包含多个单词时,单词间用一个空格分隔;第一个单词前和最后一个单词后没有空格。
若只包含一个单词,则该单词前后均无空格。
搜索条件
1、由一个或多个不重复的关键字组成,每个关键字是一个单词。
2、当包含多个关键字时,关键字间用一个空格分隔;第一个关键字前和最后一个关键字后
没有空格。
3、若只包含一个关键字,则该关键字前后均无空格。
4、关键字搜索的时候需要单词完全匹配如  关键字为soft  书名为software 则不匹配 

输入:
整数N。
N行字符串,每行一个书名。
一行字符串,搜索条件,包含一个或者多个搜索的关键词。

输出:

整数M,满足条件的书名个数(未找到则为0)
符合条件的书名,每行一个,如果多个,按到书名的字典序输出。
字典顺序
1.两个书名均从第一个单词开始逐个单词比较,若遇到不相同的单词,则单词“较小”的书名排在前面。
2.单词中字母全部为小写。两个单词先以第一个字母作为排序的基准,如果第一个字母相同,就用第二个字母
为基准,如果第二个字母相同就以第三个字母为基准。依此类推,如果到某个字母不相同,字母顺序在前的
那个单词“较小”。
3.当一个短单词和一个长单词的开头部分都相同(即短单词是长单词从首字母开始的一部分),
短单词“较小”。

样例输入:

7
high performance mysqlsecond edition
writing gnu emacs extensions
web client programming with perlautomating tasks
net test automation recipes a problem solution approach
photoreading
pro wfwindows workflow in net
aspect oriented analysis and design the theme approach
extensions gnu

样例输出:

1
writing gnu emacs extensions

答案提示:

书名个数N范围 [1,200]  
书名所含单词个数  [1,10]  
单词所含字母数  [1,50]  
搜索条件中关键字个数 [1,3]  
 

AC代码
 

#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAX=1000;

int WordCompare(const string &str1,const string &str2 )
{
	//str1<str2
	int len1=str1.length();
	int len2=str2.length();
	int len=len1<=len2 ? len1 : len2;
	for(int i=0;i<len;++i)
	{
		if(str1[i]<str2[i])
			return -1;
		else if(str1[i]>str2[i])
			return 1;
	}
	if(len1<len2)
		return -1;
	else if(len1==len2)
		return 0;
	else
		return 1;
}
bool BookNameCompare(const vector<string> &bookname1,const vector<string> &bookname2)
{
	//小于等于true
	int len1=bookname1.size();
	int len2=bookname2.size();
	int len=len1<=len2 ? len1 : len2;
	int flag;
	for(int i=0;i<len;++i)
	{
		flag=WordCompare(bookname1[i],bookname2[i]);
		if(flag==-1)
			return true;
		else if(flag==1)
			return false;
	}
	if(len1<=len2)
		return true;
	else
		return false;
}
void SortSolve(vector<vector<string> > &result_shelf)
{
	//stable_sort(vec_words.begin(),vec_words.end(),WordCompare);
	for(int i=0;i<result_shelf.size();++i)
	{
		for(int j=result_shelf.size()-1;j>i;--j)
		{
			if(BookNameCompare(result_shelf[j],result_shelf[j-1]))
			{
				vector<string> tmp=result_shelf[j];
				result_shelf[j]=result_shelf[j-1];
				result_shelf[j-1]=tmp;
			}
		}
	}
}
void PrintBook(const vector<string> &bookname)
{
	for(int i=0;i<bookname.size();++i)
	{
		cout<<bookname[i];
		if(i!=bookname.size()-1)
			cout<<' ';
	}
	cout<<endl;
}
void SearchBook(const vector<vector<string> > &shelf,const vector<string> &condition_find)
{
	vector<vector<string> > result;
	int size_condition=condition_find.size();
	int nmatch=0;
	for(int i=0;i<shelf.size();++i)
	{
		nmatch=0;
		for(int k=0;k<size_condition;++k)
		{
			for(int j=0;j<shelf[i].size();++j)
			{
				if(condition_find[k].compare(shelf[i][j])==0)
				{
					++nmatch;
					break;
				}
			}
		}
		if(nmatch==size_condition)
		{
			//找到一本
			result.push_back(shelf[i]);
		}	
	}
	if(result.size()==0)
		cout<<0<<endl;
	else
	{
		cout<<result.size()<<endl;
		SortSolve(result);
		for(int i=0;i<result.size();++i)
			PrintBook(result[i]);
	}
}
int main()
{
	int N;
	cin>>N;
	char str[MAX];
	char condition[MAX];
	vector<vector<string> > shelf;
	vector<string> bookname;
	for(int i=0;i<N+1;++i)
	{
		bookname.clear();
		//cin.clear();
		cin.getline(str,MAX);
		int len=strlen(str);
		string strTmp="";
		
		for(int j=0;j<len;++j)
		{
			if(str[j]!=' ')
				strTmp +=str[j];
			else
			{
				if(strTmp.compare("")!=0)
					bookname.push_back(strTmp);
				strTmp="";
			}
		}
		if(strTmp.compare("")!=0)
			bookname.push_back(strTmp);
		if(bookname.size()>0)
			shelf.push_back(bookname);
	}
	cin.getline(condition,MAX);
	int len=strlen(condition);
	string strTmp="";
	vector<string> condition_find;
	for(int j=0;j<len;++j)
	{
		if(condition[j]!=' ')
			strTmp +=condition[j];
		else
		{
			if(strTmp.compare("")!=0)
				condition_find.push_back(strTmp);
			strTmp="";
		}
	}
	if(strTmp.compare("")!=0)
		condition_find.push_back(strTmp);

	SearchBook(shelf,condition_find);
 	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值