-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubstrAnalyser.cpp
211 lines (193 loc) · 7.53 KB
/
SubstrAnalyser.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "SubstrAnalyser.h"
void SubstrAnalyser::Flags::make_empty() {
is_pref = true;
is_suf = true;
is_substr = true;
is_empty = true;
}
SubstrAnalyser::Flags& SubstrAnalyser::Flags::operator|=(const SubstrAnalyser::Flags& other) {
is_elem |= other.is_elem;
is_pref |= other.is_pref;
is_suf |= other.is_suf;
is_substr |= other.is_substr;
is_empty |= other.is_empty;
return *this;
}
SubstrAnalyser::Flags SubstrAnalyser::Flags::operator|(const SubstrAnalyser::Flags& other) const {
Flags result = *this;
result |= other;
return result;
}
SubstrAnalyser::Flags SubstrAnalyser::Flags::concatenate(const Flags& other) const {
Flags result;
result.is_elem = is_elem && other.is_elem;
result.is_pref = (is_elem && other.is_pref) || (is_pref && other.is_empty);
result.is_suf = is_suf && other.is_elem || (is_empty && other.is_suf);
result.is_substr =
(is_suf && other.is_pref) || (is_substr && other.is_empty) || (is_empty && other.is_substr);
result.is_empty = is_empty && other.is_empty;
return result;
}
size_t SubstrAnalyser::get_substr_index(size_t len, size_t index) {
if (len == 0) {
return 0;
}
return length_index[len] + index;
}
std::vector<SubstrAnalyser::Flags>
SubstrAnalyser::unionEntryArr(const std::vector<Flags>& first, const std::vector<Flags>& second) const {
std::vector<Flags> result(substr_number);
for (size_t i = 0; i < result.size(); ++i) {
result[i] = first[i] | second[i];
}
return result;
}
void SubstrAnalyser::unionTop(std::stack<std::vector<Flags>>& operation_order) {
if (operation_order.empty()) {
std::cerr << "Missing argument for union.\n";
throw std::invalid_argument("Missing argument for union.\n");
}
std::vector<Flags> right = std::move(operation_order.top());
operation_order.pop();
if (operation_order.empty()) {
std::cerr << "Missing argument for union.\n";
throw std::invalid_argument("Missing argument for union.\n");
}
std::vector<Flags> left = std::move(operation_order.top());
operation_order.pop();
operation_order.emplace(unionEntryArr(left, right));
}
std::vector<SubstrAnalyser::Flags> SubstrAnalyser::concatenate(const std::vector<Flags>& left,
const std::vector<Flags>& right) {
std::vector<Flags> result(substr_number);
result[0].make_empty();
for (size_t l = 0; l <= word.size(); ++l) {
for (size_t i = 0; i < word.size() - l + 1; ++i) {
size_t left_substr = get_substr_index(l, i);
size_t continuation_index = i + l;
for (size_t k = 0; k < word.size() - continuation_index + 1; ++k) {
size_t right_substr = get_substr_index(k, continuation_index);
size_t concat_substr = get_substr_index(k + l, i);
result[concat_substr] |= left[left_substr].concatenate(right[right_substr]);
}
}
}
return result;
}
void SubstrAnalyser::concatenateTop(std::stack<std::vector<Flags>>& operation_order) {
if (operation_order.empty()) {
std::cerr << "Missing argument for concatenation.\n";
throw std::invalid_argument("Missing argument for concatenation.\n");
}
std::vector<Flags> right = std::move(operation_order.top());
operation_order.pop();
if (operation_order.empty()) {
std::cerr << "Missing argument for concatenation.\n";
throw std::invalid_argument("Missing argument for concatenation.\n");
}
std::vector<Flags> left = std::move(operation_order.top());
operation_order.pop();
operation_order.emplace(concatenate(left, right));
}
std::vector<SubstrAnalyser::Flags> SubstrAnalyser::closure(const std::vector<Flags>& entry_arr) {
std::vector<Flags> result = entry_arr;
result[0].is_elem = true;
for (size_t l = 0; l <= word.size(); ++l) {
for (size_t i = 0; i < word.size() - l + 1; ++i) {
size_t left_substr = get_substr_index(l, i);
size_t continuation_index = i + l;
for (size_t k = 0; k < word.size() - continuation_index + 1; ++k) {
size_t right_substr = get_substr_index(k, continuation_index);
size_t concat_substr = get_substr_index(k + l, i);
result[concat_substr] |= result[left_substr].concatenate(result[right_substr]);
}
}
}
return result;
}
void SubstrAnalyser::closureTop(std::stack<std::vector<Flags>>& operation_order) {
if (operation_order.empty()) {
std::cerr << "Missing argument for closure operation.\n";
throw std::invalid_argument("Missing argument for closure operation.\n");
}
std::vector<Flags> entry_arr = operation_order.top();
operation_order.pop();
operation_order.emplace(closure(entry_arr));
}
std::vector<SubstrAnalyser::Flags> SubstrAnalyser::getEntryArray(char letter) {
std::vector<Flags> result(substr_number);
result[0].make_empty();
if (letter == EMPTY_SIGN) {
result[0].is_elem = true;
return result;
}
for (size_t i = 0; i <= word.size(); ++i) {
if (letter == word[i]) {
result[i + 1] = {true, true, true, true, false};
}
}
return result;
}
std::vector<SubstrAnalyser::Flags> SubstrAnalyser::getEntryArray(const std::string& regular_expr) {
std::stack<std::vector<Flags>> operation_order;
for (auto& letter: regular_expr) {
if (letter == UNION_SIGN) {
unionTop(operation_order);
continue;
}
if (letter == CONCATENATION_SIGN) {
concatenateTop(operation_order);
continue;
}
if (letter == CLOSURE_SIGN) {
closureTop(operation_order);
continue;
}
operation_order.emplace(getEntryArray(letter));
}
if (operation_order.size() != 1) {
std::cerr << "Wrong format, too few operations.\n";
throw std::invalid_argument("Wrong format, too few operations.\n");
}
return operation_order.top();
}
SubstrAnalyser::SubstrAnalyser(const std::string& word) : word(word), length_index(word.size() + 1),
substr_number(
word.size() + word.size() * (word.size() - 1) / 2 +
1) {
substrings.emplace_back("");
length_index[0] = 0;
length_index[1] = 1;
for (size_t l = 2; l <= word.size(); ++l) {
length_index[l] = length_index[l - 1] + word.size() - (l - 1) + 1;
}
for (size_t l = 1; l <= word.size(); ++l) {
for (size_t i = 0; i < word.size() - l + 1; ++i) {
substrings.emplace_back(word.begin() + i, word.begin() + i + l);
}
}
}
size_t SubstrAnalyser::findMaxSubstr(const std::string& regular_expr) {
std::vector<Flags> entry_arr = getEntryArray(regular_expr);
size_t max_size = word.size();
for (int32_t i = substr_number - 1; i >= 0; --i) {
if (length_index[max_size] > i) {
--max_size;
}
if (entry_arr[i].is_substr) {
break;
}
}
return max_size;
}
size_t find_max_substr(const std::string& reg_expr, const std::string& word) {
SubstrAnalyser helper(word);
return helper.findMaxSubstr(reg_expr);
}
void stream_in_out(std::istream& in, std::ostream& out) {
std::string regular_expr;
std::string word;
in >> regular_expr >> word;
SubstrAnalyser helper(word);
out << helper.findMaxSubstr(regular_expr);
}