中间断断续续很多题没有更新解法,趁着病毒肆虐出不了门,就把这些都写了。
代码之前都写好也通过了,这里就大致说一下算法,细节的问题能想起来就写把~
这道题属于看着文字很复杂,输入很繁琐,但是实际做起来还不难。
题目就是要输入一堆书,然后按照要求进行检索,并且检索结果要根据id大小顺序输出。
依旧是,编写sort()函数的排序规则,先把所有书进行排列,排序的依据就是id大小,然后检索的时候从头开始检索,检索到就直接输出就行了。
关键词由于可以最多包含5个,别的使用普通的对比就行了,关键词这里就使用strstr函数。实际上严格说来这样做是有风险的,比如我们需要找一本关键词为man的书,但是某本书里面包含了human或这woman,都会被检索出来,更加严格的做法是检索出来以后还要判断是否是独立单词,但是毕竟这样也能过,也就没有追究那么多了
上代码!
#include <iostream>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
struct book{
string inf[6];
};
struct book all[10001];
string tsearch[1001];
int N, M;
bool cmp(struct book a, struct book b){return a.inf[0] < b.inf[0];}
int main()
{
scanf("%d", &N);
getline(cin, all[0].inf[0]);
for(int i=0;i<N;i++)
{
for(int j=0;j<6;j++)
getline(cin, all[i].inf[j]);
}
sort(all, all+N, cmp);
cin >> M;
getline(cin, tsearch[0]);
for(int i=0;i<M;i++)
getline(cin, tsearch[i]);
for(int i=0;i<M;i++)
{
cout << tsearch[i] << endl;
int temp = ((int)tsearch[i][0]) - '0';
tsearch[i].erase(tsearch[i].begin(), tsearch[i].begin()+3);
if(temp != 3)
{
int flag = 0;
for(int j=0;j<N;j++)
{
if(all[j].inf[temp] == tsearch[i])
{
cout << all[j].inf[0] << endl;
flag = 1;
}
}
if(flag == 0)
cout << "Not Found" << endl;
}
else
{
int flag = 0;
for(int j=0;j<N;j++)
{
if(strstr(all[j].inf[3].c_str(), tsearch[i].c_str()))
{
cout << all[j].inf[0] << endl;
flag = 1;
}
}
if(flag == 0)
cout << "Not Found" << endl;
}
}
return 0;
}