Print out all possible fixed length words

Tomilov Anatoliy

I want to print out all possible words (generating input for testing of other algorithm) of length n, containing the (maybe duplicated) letters from 'a' to 'a' + n.

I tried to do the following:

#include <iostream>
#include <string>
#include <algorithm>

using size_type = std::size_t;
using symbol_type = std::string;
using char_type = typename symbol_type::value_type;

template< size_type n >
struct test
{

    static_assert(!(size_type('z' - 'a') + 1 < n));

    void
    print(symbol_type const & _symbol) const
    {
        for (size_type i = 0; i < n; ++i) {
            std::cout << _symbol.substr(i * n, n) << std::endl;
        }
        std::cout << std::endl;
    }

    bool
    operator () () const
    {
        symbol_type mishmash_;
        for (size_type i = 0; i < n; ++i) {
            mishmash_.append(symbol_type(n, char_type('a' + i)));
        }
        print(mishmash_);
        while (std::next_permutation(std::begin(mishmash_), std::end(mishmash_))) {
            print(mishmash_);
        }
        return true;
    }

};

int
main()
{
    test< 3 > const test_{};
    if (test_()) {
        std::cout << "Succes!" << std::endl;
        return EXIT_SUCCESS;
    } else {
        std::cerr << "Failure!" << std::endl;
        return EXIT_FAILURE;
    }
}

But there are duplicates of words. How to achive the desired in most optimal way?

Tony Delroy

It's conceptually the same task as iterating the numbers from 0000000... (n times) to 999999... - just doing a base-n conversion: i.e. increment a number and each time you want the next "value", use % n + 'a' to extract a "digit" and /= n to prepare to get the next one....

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python strftime %A fixed length

来自分类Dev

Print issue with bootstrap fixed navbar

来自分类Dev

string to array, max length, break on words

来自分类Dev

Is it possible to print without using the print function in Python?

来自分类Dev

How to print variable length lists as columns in python?

来自分类Dev

在ifelse函数中更改length.out

来自分类Dev

Fixed length, 6-bit binary string in perl

来自分类Dev

Increment all records with fixed value in rails

来自分类Dev

Hiding all fixed-position elements

来自分类Dev

Is there a way to print all velocity context?

来自分类Dev

首选的System.out.print()或System.out.printf()?

来自分类Dev

print out 5 characters in a line bash scripting

来自分类Dev

嵌套的“ System.out.print”输出

来自分类Dev

Is there a way to print a Groovy Collection out in a pastable format?

来自分类Dev

System.out.print导致延迟?

来自分类Dev

Out.Print方法不起作用?

来自分类Dev

Finding all groups of contiguous words in string

来自分类Dev

Fade only top part of div out as it reaches fixed navigation

来自分类Dev

Absolute positioned div sticking out of a scrollable fixed position div

来自分类Dev

Is it possible to have full width JSSOR slider with fixed height?

来自分类Dev

How to find possible English words in long random string?

来自分类Dev

R:seq()函数的“ length.out”参数无法正常工作

来自分类Dev

匹配精确单词与stri_replace_all_fixed

来自分类Dev

Why does my sort print out multiple lists?

来自分类Dev

How to print out the file name and line number of the test in python nose?

来自分类Dev

是System.out.print中和FXML报表如何使用“%”符号

来自分类Dev

out.print出现在html标记之外

来自分类Dev

测试程序中的调试语句的Logger或System.out.print

来自分类Dev

无法比较out.print()返回的字符串

Related 相关文章

  1. 1

    Python strftime %A fixed length

  2. 2

    Print issue with bootstrap fixed navbar

  3. 3

    string to array, max length, break on words

  4. 4

    Is it possible to print without using the print function in Python?

  5. 5

    How to print variable length lists as columns in python?

  6. 6

    在ifelse函数中更改length.out

  7. 7

    Fixed length, 6-bit binary string in perl

  8. 8

    Increment all records with fixed value in rails

  9. 9

    Hiding all fixed-position elements

  10. 10

    Is there a way to print all velocity context?

  11. 11

    首选的System.out.print()或System.out.printf()?

  12. 12

    print out 5 characters in a line bash scripting

  13. 13

    嵌套的“ System.out.print”输出

  14. 14

    Is there a way to print a Groovy Collection out in a pastable format?

  15. 15

    System.out.print导致延迟?

  16. 16

    Out.Print方法不起作用?

  17. 17

    Finding all groups of contiguous words in string

  18. 18

    Fade only top part of div out as it reaches fixed navigation

  19. 19

    Absolute positioned div sticking out of a scrollable fixed position div

  20. 20

    Is it possible to have full width JSSOR slider with fixed height?

  21. 21

    How to find possible English words in long random string?

  22. 22

    R:seq()函数的“ length.out”参数无法正常工作

  23. 23

    匹配精确单词与stri_replace_all_fixed

  24. 24

    Why does my sort print out multiple lists?

  25. 25

    How to print out the file name and line number of the test in python nose?

  26. 26

    是System.out.print中和FXML报表如何使用“%”符号

  27. 27

    out.print出现在html标记之外

  28. 28

    测试程序中的调试语句的Logger或System.out.print

  29. 29

    无法比较out.print()返回的字符串

热门标签

归档