单词的组合......权力集?

时间:2014-08-21 02:28:55

标签: python c++ powerset

我想破解我忘记的密码。我知道密码中可能使用的所有单词,但有些可能会被使用或不使用。示例:HarryMetSally可能是密码,但我可能会使用“Harry”“Billy”“Sally”和“Met”的词汇表来组合破解。

如果我有16个单词但可能使用了14-16个,我将如何用C ++或Python编写16个单词(或20个,25个,30个单词)随机连接在一起并一次使用1个或2-16个甚至不在同一个顺序。

示例:

单词:Harry Billy Sally Met

组合示例:

哈利 比利 SallyBilly BillySally HarryMetSally HarrySallyMet

我已经广泛搜索了互联网并尝试了Excel论坛。有什么指导吗?

1 个答案:

答案 0 :(得分:0)

你说随机......

#include <iostream>
#include "stdlib.h"
#include "time.h"

#define NUM_WORDS 3
char* words[NUM_WORDS] = {
    "Foo",
    "Bar",
    "Baz"
};
typedef unsigned long long ULL;

ULL fact(ULL n, ULL lowerLimit = 1)
{
    if (n <= lowerLimit) return 1;
    return n * fact(n - 1);
}
int main()
{
    srand(time(NULL));

    // I can try single words all by myself, so start at 2 or more words
    for (int nWords = 2; nWords <= NUM_WORDS; ++nWords)
    {
        // calculate the number of possibilities
        // but since it's random, triple it for good measure
        ULL nIters = fact(NUM_WORDS, NUM_WORDS - nWords) * 3;
        for (ULL iter = 0; iter < nIters; ++iter)
        {
            for (int iterWord = 0; iterWord < nWords; ++iterWord)
            {
                int ix = rand() % NUM_WORDS;
                std::cout << words[ix];
            }
            std::cout << std::endl;
        }
    }
    return 0;
}
相关问题