-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.cpp
125 lines (114 loc) · 2.82 KB
/
search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Do NOT add any other includes
#include "search.h"
senten::senten(int book_code, int page, int paragraph, int sentence_no, string sentence)
{
b_code = book_code;
pg = page;
para = paragraph;
sen_no = sentence_no;
for (int i = 0; i < sentence.size(); i++)
{
int ascii = static_cast<int>(sentence[i]);
if (ascii <= 90 && ascii >= 65)
{
char r = static_cast<char>(ascii + 32);
sen.push_back(r);
}
else
{
sen.push_back(sentence[i]);
}
}
}
vector<int> LPS(string s)
{
int n = s.size();
vector<int> pi(n, 0);
for (int i = 1; i < n; i++)
{
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
{
j = pi[j - 1];
}
if (s[i] == s[j])
{
j++;
}
pi[j] = j;
}
return pi;
}
vector<int> search1(vector<int> prefix_arr, vector<char> test, string pattern)
{
vector<int> res;
int pos = -1;
int i = 0;
int j = 0;
while (i < test.size())
{
if (test[i] == pattern[j])
{
j++;
i++;
}
else
{
if (j != 0)
{
j = prefix_arr[j - 1];
}
else
{
i++;
}
}
if (j == pattern.size())
{
pos = i - pattern.size();
res.push_back(pos);
}
}
return res;
}
SearchEngine::SearchEngine()
{
// Implement your function here
}
SearchEngine::~SearchEngine()
{
for (int i = 0; i < texts.size(); i++)
{
delete texts[i];
}
}
void SearchEngine::insert_sentence(int book_code, int page, int paragraph, int sentence_no, string sentence)
{
senten *sent = new senten(book_code, page, paragraph, sentence_no, sentence);
texts.push_back(sent);
return;
}
Node* SearchEngine::search(string pattern, int &n_matches) {
vector<int> prefix_arr = LPS(pattern);
Node *head = nullptr;
Node *current = nullptr;
int n_mat = 0;
for (int i = 0; i < texts.size(); i++) {
vector<char> temp = texts[i]->sen;
vector<int> p = search1(prefix_arr, temp, pattern);
for (int k = 0; k < p.size(); k++) {
n_mat++;
Node *newNode = new Node(texts[i]->b_code, texts[i]->pg, texts[i]->para, texts[i]->sen_no, p[k]);
if (current == nullptr) {
head = newNode;
current = newNode;
} else {
current->right = newNode;
newNode->left = current;
current = newNode;
}
}
}
n_matches = n_mat;
return head;
}