-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords_lib.cpp
201 lines (177 loc) · 4.81 KB
/
words_lib.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
#include "words_lib.h"
using namespace std;
void WordsParser::parse(const string& s)
{
const size_t delim_len = m_delim.length();
size_t pos_start = 0, pos_end;
while ((pos_end = s.find(m_delim, pos_start)) != string::npos) {
string word = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
m_words.push_back(move(word));
}
m_words.push_back(s.substr(pos_start));
}
void WordCount::process()
{
for (const auto &w : m_parser.get_words())
{
++m_occurrences[w];
}
}
void WordCount::print(ostream &out) const
{
out << "Count of words: " << m_occurrences.size() << "\n";
for (const auto& p : m_occurrences)
{
out << p.first << "\t-\t" << p.second << " occurrences\n";
}
}
void LongestWord::process()
{
const auto &words = m_parser.get_words();
auto longest_it = words.cbegin();
for (auto it = words.cbegin() + 1; it != words.cend(); ++it)
{
if (it->size() > longest_it->size())
longest_it = it;
}
m_res = *longest_it;
}
void LongestWord::print(ostream& out) const
{
out << "Longest word: " << m_res << "\n";
}
void LongestRepeatableSequenceWord::process()
{
for (const auto& w : m_parser.get_words())
{
string lrs = longest_repeated_substring(w);
if (lrs.size() > m_sequence.size())
{
m_sequence = move(lrs);
m_word = w;
}
}
}
void LongestRepeatableSequenceWord::print(ostream& out) const
{
out << "Word with longest repeatable sequence: " << m_word << "(" << m_sequence << ")" << "\n";
}
string LongestRepeatableSequenceWord::longest_repeated_substring(const string &str)
{
// Dynamic Programming O(n^2) solution from
// https://www.geeksforgeeks.org/longest-repeating-and-non-overlapping-substring/
const size_t n = str.length();
vector<vector<size_t>> LCSRe;
LCSRe.resize(n + 1);
for (auto &v : LCSRe)
{
v.resize(n + 1);
}
string res; // To store result
size_t res_length = 0; // To store length of result
// building table in bottom-up manner
size_t i, index = 0;
for (i = 1; i <= n; i++)
{
for (size_t j = i + 1; j <= n; j++)
{
// (j-i) > LCSRe[i-1][j-1] to remove
// overlapping
if (str[i - 1] == str[j - 1] &&
LCSRe[i - 1][j - 1] < (j - i))
{
LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1;
// updating maximum length of the
// substring and updating the finishing
// index of the suffix
if (LCSRe[i][j] > res_length)
{
res_length = LCSRe[i][j];
index = max(i, index);
}
}
else
LCSRe[i][j] = 0;
}
}
// If we have non-empty result, then insert all
// characters from first character to last
// character of string
if (res_length > 0)
for (i = index - res_length + 1; i <= index; i++)
res.push_back(str[i - 1]);
return res;
}
void ReverseWordsOrder::process()
{
const auto& words = m_parser.get_words();
m_reversed_words.reserve(words.size());
for (auto it = words.rbegin(); it != words.rend(); ++it)
{
m_reversed_words.push_back(*it);
}
}
void ReverseWordsOrder::print(ostream& out) const
{
out << "Words in reversed order: ";
for (const auto& w : m_reversed_words)
{
out << w << " ";
}
}
void Sort::process()
{
m_sorted_words = m_parser.get_words();
const size_t size = m_sorted_words.size();
// bubble sort to fit the requirement not to use existing std func to do the whole work
for (size_t i = 0; i < size - 1; i++)
{
for (size_t j = 0; j < size - i - 1; j++)
{
if (comparator(m_sorted_words[j], m_sorted_words[j + 1]))
{
swap(m_sorted_words[j], m_sorted_words[j + 1]);
}
}
}
}
void Sort::print(ostream& out) const
{
out << "Sorded words(asc): ";
for (const auto& w : m_sorted_words)
{
out << w << " ";
}
}
void SortDesc::print(std::ostream& out) const
{
out << "Sorded words(desc): ";
for (const auto& w : get_words())
{
out << w << " ";
}
}
void ReverseLetters::process()
{
const auto& words = m_parser.get_words();
m_words.reserve(words.size());
for (const auto& w : words)
{
string reversed_w;
reversed_w.reserve(w.size());
for (auto it = w.rbegin(); it != w.rend(); ++it)
{
reversed_w.push_back(*it);
}
m_words.push_back(move(reversed_w));
}
}
void ReverseLetters::print(ostream& out) const
{
out << "Reversed letters: ";
for (const auto& w : m_words)
{
out << w << " ";
}
}