如何生成字典的字符序列?

问题描述 投票:0回答:1

我想使用插入函数来生成字符序列,但是无法编写或搜索它。

我尝试使用https://www.tutorialspoint.com/cplusplus-program-to-generate-a-sequence-of-n-characters-for-a-given-specific-case,但是它对我不起作用,并且出现“ segfault”。

我已经使用过类似的资金,但是上面的解决方案给我带来了段错误。

    string = salt, b_salt, outTrip, buffer;
    string dict = ".012345678A:;<=>?@BCDEFGHIJKLMNOPQRSTUVWXY[\\]^_`ZabcdefghijklmnopqrstuvwxyzT"
    for(i = 0; i < n; i++)
    {
        buffer = generateSeq(dict)
        b_salt = buffer;
        b_salt += "...";

        salt = b_salt.substr(1,2);
        s_test = crypt(buffer.c_str(), salt.c_str());

        outTrip = s_test;

        cout << seq << "\t\t\t" << outTrip << endl;

我的代码:

#include <iostream>
#include <unistd.h>
#include <cstring>

using namespace std;

void GenSequence(char str[], int m, int len, char *seq)
{
    int i, j=0, k, index, r;
    for(i = 0; i < m; i++)
    {
        if(j == 0) {
            seq[j++] = str[rand()%len];}
        else
        {
            h:
            index = rand()%len;
            for(k = 0; k < j; k++)
            {
                if(str[index] == seq[k])
                    goto h;
            }
            seq[j++] = str[index];
        }
    }
    seq[j] = '\0';
}

int main() 
{   
    int n, m, len, i;
    char str[] = {46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 65, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 90, 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};
    n = 1000;
    m = 7;
    len = strlen(str);
    string trip, match = "shi", buf, b_salt, outTrip, salt;
    char seq[m];
    for(i = 0; i < n; i++)
    {
        outTrip = "";
        GenSequence(str, m, len, seq);
        b_salt = seq;
        b_salt += "...";

        salt = b_salt.substr(1,2);
        outTrip = crypt(seq, salt.c_str());


        cout << seq << "\t\t\t" << outTrip << endl;

    }
}

解决方案:我删除了一些错误的字符,即字符串字典=“ .0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;它对我有用。

c++ algorithm
1个回答
0
投票

我删除了一些错误的字符,即

string dict = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; and 

为我工作。

© www.soinside.com 2019 - 2024. All rights reserved.