Words from neighbour ones: Difference between revisions
Content added Content deleted
(C code efficiency improved) |
(C++ code efficiency improvement) |
||
Line 187: | Line 187: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
|||
<lang cpp>#include <algorithm> |
<lang cpp>#include <algorithm> |
||
#include <cstdlib> |
#include <cstdlib> |
||
Line 194: | Line 195: | ||
#include <string> |
#include <string> |
||
#include <vector> |
#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) { |
int main(int argc, char** argv) { |
||
const int min_length = 9; |
const int min_length = 9; |
||
Line 204: | Line 207: | ||
} |
} |
||
std::string line; |
std::string line; |
||
boost::circular_buffer<std::string> words(min_length); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
std::sort(words.begin(), words.end()); |
|||
std::string previous_word; |
std::string previous_word; |
||
int count = 0; |
int count = 0; |
||
⚫ | |||
⚫ | |||
⚫ | |||
continue; |
|||
⚫ | |||
⚫ | |||
continue; |
|||
std::string word; |
std::string word; |
||
word.reserve(min_length); |
word.reserve(min_length); |
||
for (size_t |
for (size_t i = 0; i < min_length; ++i) |
||
word += words[i |
word += words[i][i]; |
||
if (previous_word == word) |
if (previous_word == word) |
||
continue; |
continue; |
||
auto |
auto it = std::find(words.begin(), words.end(), word); |
||
if ( |
if (it != words.end()) |
||
std::cout << std::setw(2) << ++count << ". " << word << '\n'; |
std::cout << std::setw(2) << ++count << ". " << word << '\n'; |
||
previous_word = word; |
previous_word = word; |