1022. Digital Library 解析

注意一行句子的输入方法。

_(:з)∠)_这个题我想复杂了。大家注意关键字行的key是一个个词,不要理解成一句话了。我理解成一话用KMP和字符串匹配来做一直超时。还弄的很麻烦。

题目给的查找的 能找到的一定是能匹配的。

本来挺容易的一道的被我弄复杂了。


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

#define MAX  10010
#define MAX2 1010

using namespace std;


int N;

struct Node {
	string id;
	string title;
	string author;
	string key;
	string publisher;
	string year;
	vector <string> keyword;
};
Node lib[MAX];

struct Text{
	int No;
	string data;
};
Text text[MAX2];


bool Compare(string s1, string s2) {//s1模式串 s2匹配串
//	cout << s1 << " s2: " << s2 << " ";
	int count = 0;
	if (s2.size() > s1.size())
		return false;
	else{
		int i = 0, j = 0;
		int tempi = i;
		while (i < s1.size() && j < s2.size()) {
			if (s1[tempi] == s2[j])
				tempi++, j++;
			else {
				i++, tempi = i, j = 0, count = 0;
			}
		}
//		cout << " j: " << j;
		if (j == s2.size()) {
//			cout << " Ture" << endl;
			return true;
		}
		else {
//			cout << " False" << endl;
			return false;
		}
	}

}

bool cmp(string N1 ,string N2 ){
	return N1 < N2;
}

int Next[MAX2];

void getNext(string s1) {
	int j = -1;
	Next[0] = -1;
	for (int i = 1; i < s1.size(); i++) {
		while (j != -1 && s1[i] != s1[j + 1]) {
			j = Next[j];
		}
		if (s1[i] == s1[j + 1]) {
			j++;
		}
		if (j == -1 || s1[i + 1] != s1[j + 1]) {
			Next[i] = j;
		}
		else {
			Next[i] = Next[j];
		}
	}
}

bool KMP(string text, string pattern) {
	int n = text.size(), m = pattern.size();
	getNext(pattern);
	/*for (int i = 0; i < m; i++) {
		cout << Next[i] << endl;
	}*/
	int j = -1;
	for (int i = 0; i < n; i++) {
		while (j != -1 && text[i] != pattern[j + 1]) {
			j = Next[j];
		}
		if (text[i] == pattern[j + 1]) {
			j++;
		}
		if (j == m - 1) {
			return true;
		}
	}
	return false;
}


void Search(int num ,Text t) {
	string s;
	string rank[MAX2];
	bool tag = false;
	int count = 0;

	for (int i = 0; i < N; i++) {

		switch (num) {
		case 1:s = lib[i].title; break;
		case 2:s = lib[i].author; break;
		case 3:s = lib[i].key; break;
		case 4:s = lib[i].publisher; break;
		case 5:s = lib[i].year; break;
		}


		if (t.No != 3) {
			if (s == t.data) {
//				cout << s << " " << t.data << endl;
				tag = true;
				rank[count] = lib[i].id;
				count++;
			}
		}
		else if(t.No == 3 ){
			for (int j = 0; j < lib[i].keyword.size(); j++) {
				if (t.data == lib[i].keyword[j]) {
					rank[count] = lib[i].id;
					tag = true;
					count++;
				}
			}
		}
	}

	cout << t.No << ": " << t.data << endl;
	if (tag) {
		sort(rank, rank + count, cmp);
		for (int i = 0; i < count; i++) {
			cout << rank[i] << endl;
		}
	}
	else{
		cout << "Not Found" << endl;
	}
	
}




int main() {
	cin >> N;
	cin.get();

//	ios_base::sync_with_stdio(false);

	int times = 0;
	for (int i = 0; i < N; i++) {
		times = 0;
		getline(cin, lib[i].id, '\n');
		getline(cin,lib[i].title,'\n');
		getline(cin,lib[i].author,'\n');
		getline(cin,lib[i].key,'\n');
		getline(cin,lib[i].publisher,'\n');
		getline(cin, lib[i].year, '\n');
		int j = 0; char c; string s; int z = 0;
		while (lib[i].key[j] != '\0') {
			c = lib[i].key[j];
			if (c != ' ') {
				s.push_back(c);
				z++;
			}
			else {
				lib[i].keyword.push_back(s);
				s.clear();
				z = 0;
			}
			j++;
		}
		lib[i].keyword.push_back(s);

	}

	cin.clear();
	//for (int i = 0; i < N; i++) {
		cout << lib[i].id << endl;
		cout << lib[i].title << endl;
		cout << lib[i].author << endl;
		cout << lib[i].key << endl;
		cout << lib[i].publisher << endl;
		cout << lib[i].year << endl;
	//	cout << "i : " << i << endl;
	//	for (int j = 0; j < lib[i].keyword.size(); j++) {
	//		cout << lib[i].keyword[j] << endl;
	//	}
	//}


	int M;
	cin >> M;
	cin.get();

	for (int i = 0; i < M; i++) {
		cin >> text[i].No;
		cin.get();
		cin.get();
		getline(cin, text[i].data, '\n');
	}

	//for (int i = 0; i < M; i++) {
	//	cout << text[i].No << endl;
	//	cout << text[i].data << endl;
	//}

	for (int i = 0; i < M; i++) {
		Search(text[i].No, text[i]);
	}
	

	system("pause");

	return 0;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值