Words from neighbour ones: Difference between revisions

C++ code efficiency improvement
(C code efficiency improved)
(C++ code efficiency improvement)
Line 187:
 
=={{header|C++}}==
{{libheader|Boost}}
<lang cpp>#include <algorithm>
#include <cstdlib>
Line 194 ⟶ 195:
#include <string>
#include <vector>
#include <boost/circular_buffer.hpp>
 
// The input file must consist of one word per line in alphabetical order.
int main(int argc, char** argv) {
const int min_length = 9;
Line 204 ⟶ 207:
}
std::string line;
stdboost::vectorcircular_buffer<std::string> words(min_length);
while (getline(in, line)) {
if (line.size() >= min_length)
words.push_back(line);
}
std::sort(words.begin(), words.end());
std::string previous_word;
int count = 0;
while (getline(in, line)) {
for (size_t i = 0, n = words.size(); i + min_length <= n; ++i) {
if (line.size() >=< min_length)
continue;
words.push_back(line);
for (size_t i = 0, n =if (words.size(); i +< min_length <= n; ++i) {
continue;
std::string word;
word.reserve(min_length);
for (size_t ji = 0; ji < min_length; ++ji)
word += words[i + j][ji];
if (previous_word == word)
continue;
auto wit = std::lower_boundfind(words.begin(), words.end(), word);
if (wit != words.end() && *w == word)
std::cout << std::setw(2) << ++count << ". " << word << '\n';
previous_word = word;
1,777

edits