描述:
实现一个开放的书名检索库,库中存储了若干个书名
用户可以:
1. 通过接口加入书名
2. 指定搜索条件搜索库中符合条件的书名
重要格式说明
单词:
由小写英文字母组成,不含其它字符
书名:
1. 由一个或多个单词组成
2. 当包含多个单词时,单词间用一个空格分隔
3. 第一个单词前和最后一个单词后没有空格
4. 若只包含一个单词,则该单词前后均无空格
搜索条件:
1. 由一个或多个不重复的关键字组成,每个关键字是一个单词。
2. 当包含多个关键字时,关键字间用一个空格分隔;第一个关键字前和最后一个关键字后没
有空格
3. 若只包含一个关键字,则该关键字前后均无空格
4. 搜索包含指定关键字的书名,输出不需要排序(不影响自动阅卷)
5. 若搜索条件包含多个关键字,它们之间是“与”的关系,即书名中要同时包含所有指定的关
键字(但不限制关键字在书名中出现的位置和顺序)
6. 必须是“全词匹配”,即书名中的单词和关键字完全一致,例如:关键字为man,书名中包
括单词woman,则不认为该书名符合搜索要求
输出说明:
1. 如果没有查找到书名,那么输出一对双引号: “”
2. 如果存在多行输出(查找到多本书名),那么输出结果按字典序排序
举例
输入:
AddBooks
“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”
SearchBooks
“extensions gnu”
End
输出:
“writing gnu emacs extensions”
输入:
AddBooks
“high performance mysqlsecond edition”
“writing gnu emacsextensions”
“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”
SearchBooks
“approach”
End
输出:
“aspect oriented analysis and design the theme approach”
“net test automation recipes a problem solution approach”
规格
0<=书名个数范围<=200
1<=书名所含单词个数<=10
1<=单词所含字母数<=50
1<=搜索条件中关键字个数<=3
输入:
AddBooks
[书名列表:每行一个书名,书名在双引号中]
SearchBooks
[关键字:多个关键字用空格分隔,关键字在双引号中]
End
输出:
1. 查找到的书名,查找到多个书名,以多行显示,书名在双引号内
2. 如果没有查找到书名,那么输出: “”
3. 如果存在多行输出(查找到多本书名),那么输出结果按字典序排序
如下:
“aspect oriented analysis and design the theme approach”
“net test automation recipes a problem solution approach”
字母a在字典序中排在字母n前面,所以显示的时候 “aspect oriented analysis and design
the theme approach”排在前面;
如果一个字符相等,那么顺序比较后面的字符,直到找到一个字符不相等为止
样例输入:
AddBooks
“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”
SearchBooks
“extensions gnu”
End
样例输出:
实现一个开放的书名检索库,库中存储了若干个书名
用户可以:
1. 通过接口加入书名
2. 指定搜索条件搜索库中符合条件的书名
重要格式说明
单词:
由小写英文字母组成,不含其它字符
书名:
1. 由一个或多个单词组成
2. 当包含多个单词时,单词间用一个空格分隔
3. 第一个单词前和最后一个单词后没有空格
4. 若只包含一个单词,则该单词前后均无空格
搜索条件:
1. 由一个或多个不重复的关键字组成,每个关键字是一个单词。
2. 当包含多个关键字时,关键字间用一个空格分隔;第一个关键字前和最后一个关键字后没
有空格
3. 若只包含一个关键字,则该关键字前后均无空格
4. 搜索包含指定关键字的书名,输出不需要排序(不影响自动阅卷)
5. 若搜索条件包含多个关键字,它们之间是“与”的关系,即书名中要同时包含所有指定的关
键字(但不限制关键字在书名中出现的位置和顺序)
6. 必须是“全词匹配”,即书名中的单词和关键字完全一致,例如:关键字为man,书名中包
括单词woman,则不认为该书名符合搜索要求
输出说明:
1. 如果没有查找到书名,那么输出一对双引号: “”
2. 如果存在多行输出(查找到多本书名),那么输出结果按字典序排序
举例
输入:
AddBooks
“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”
SearchBooks
“extensions gnu”
End
输出:
“writing gnu emacs extensions”
输入:
AddBooks
“high performance mysqlsecond edition”
“writing gnu emacsextensions”
“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”
SearchBooks
“approach”
End
输出:
“aspect oriented analysis and design the theme approach”
“net test automation recipes a problem solution approach”
规格
0<=书名个数范围<=200
1<=书名所含单词个数<=10
1<=单词所含字母数<=50
1<=搜索条件中关键字个数<=3
输入:
AddBooks
[书名列表:每行一个书名,书名在双引号中]
SearchBooks
[关键字:多个关键字用空格分隔,关键字在双引号中]
End
输出:
1. 查找到的书名,查找到多个书名,以多行显示,书名在双引号内
2. 如果没有查找到书名,那么输出: “”
3. 如果存在多行输出(查找到多本书名),那么输出结果按字典序排序
如下:
“aspect oriented analysis and design the theme approach”
“net test automation recipes a problem solution approach”
字母a在字典序中排在字母n前面,所以显示的时候 “aspect oriented analysis and design
the theme approach”排在前面;
如果一个字符相等,那么顺序比较后面的字符,直到找到一个字符不相等为止
样例输入:
AddBooks
“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”
SearchBooks
“extensions gnu”
End
样例输出:
“writing gnu emacs extensions”
说明,此代码没有提交到OJ验证。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>
#include <map>
#include <cmath>
using namespace std;
vector<string> books;
vector<string> search_list;
int Integer_Partition(int m,int n)//整数划分
{
if (m == 1 || n == 1)
{
return 1;
}
if (n >= m)
{
return 1+Integer_Partition(m,m-1);
}
return Integer_Partition(m-n,n)+Integer_Partition(m,n-1);
}
bool comp(string a, string b)
{
int len = a.size()<b.size()?a.size():b.size();
for (int i = 0; i < len; i++)
{
if (a[i] != b[i])
{
return a[i]<b[i];
}
}
return a.size()<b.size();
}
bool Ismatched(string book_item, vector<string> key_list)
{
int num_keys = key_list.size();
map<string, int> book_map;
string word;
istringstream iss(book_item);
while(iss>>word)
{
book_map[word]++;
}
for (int i = 0; i < num_keys; i++)
{
if (book_map[key_list[i]] <= 0)
{
return false;
}
book_map[key_list[i]]--;
}
return true;
}
vector<string> find_books(string key)
{
vector<string> ret;
ret.clear();
vector<string> key_list;
int matched = 0;
istringstream iss(key);
string key_word;
while(iss>>key_word)
{
key_list.push_back(key_word);
//cout<<key_word<<endl;
}
for (int i = 0; i < books.size(); i++)
{
if (Ismatched(books[i], key_list))
{
ret.push_back(books[i]);
//cout<<"matched books: "<<books[i]<<endl;
}
}
return ret;
}
int main()
{
string lines;
getline(cin, lines);
while(getline(cin, lines) && lines != "SearchBooks")
{
lines = lines.substr(1, lines.size()-2);
books.push_back(lines);
}
while(getline(cin, lines) && lines != "End")
{
lines = lines.substr(1, lines.size()-2);
search_list.push_back(lines);
}
int sz = search_list.size();
for (int i = 0; i < sz; i++)
{
vector<string> ret ;
ret.clear();
ret = find_books(search_list[i]);
if (ret.size() == 0)
{
cout<<"\"\""<<endl;
continue;
}
if (ret.size() == 1)
{
cout<<"\""<<ret[0]<<"\""<<endl;
continue;
}
sort(ret.begin(), ret.end(), comp);
for (int j = 0; j < ret.size(); j++)
{
cout<<"\"";
cout<<ret[j]<<"\""<<endl;
}
}
system("pause");
return 0;
}