Efficiently creating a sequential wordlist from charset

非 Y 不嫁゛ 提交于 2019-12-12 12:26:36

问题


I need to find an efficient way to create a list of sequential words created from a given charset in C/C++. Let me give you an example:

If the charset is "abc", the algorithm should output:

  a
  b
  c
 aa
 ab
 ac
 ba
 bb
 bc
 ca
 cb
 cc
aaa
aab 
... 

I have some ideas, but all are require too much maths and I really need a fast solution. Who's got an idea?


回答1:


#include <stdio.h>
#include <string.h>

char* numToColumn(int n, char* outstr, const char* baseset){
    char* p = outstr;
    int len;
    len = strlen(baseset);
    while(n){
        *p++ = baseset[0 + ((n % len == 0)? len : n % len) - 1];
        n = (n - 1) / len;
    }
    *p = '\0';
    return strrev(outstr);//strrev isn't ANSI C
}

char* incrWord(char* outstr, const char* baseset){
    char *p;
    int size,len;
    int i,carry=1;

    size = strlen(baseset);
    len = strlen(outstr);
    for(i = len-1; carry && i>=0 ;--i){
        int pos;
        pos = strchr(baseset, outstr[i]) - baseset;//MUST NOT NULL
        pos += 1;//increment
        if(pos == size){
            carry=1;
            pos = 0;
        } else {
            carry=0;
        }
        outstr[i]=baseset[pos];
    }
    if(carry){
        memmove(&outstr[1], &outstr[0], len+1);
        outstr[0]=baseset[0];
    }
    return outstr;
}

int main(){
    const char *cset = "abc";
    char buff[16];
    int i;

    for(i=1;i<16;++i)//1 origin
        printf("%s\n", numToColumn(i, buff, cset));

    strcpy(buff, "cc");//start "cc"
    printf("\nrestart\n%s\n", buff);
    printf("%s\n", incrWord(buff, cset));
    printf("%s\n", incrWord(buff, cset));
    return 0;
}
/* RESULT:
a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
aab
aac

restart
cc
aaa
aab
*/



回答2:


This is really a slight modification on this answer:

What is optimal algorithm to make all possible combinations of a string?

With the above answer you could put a wrapper around the routine that essentially does permutations of the main input string letting the perutation finder find all the permutations of your pre-permuted input strings.




回答3:


The following java code works fine.

class Combination
{
static String word;
static int length;
static int[] num;
public static void main(String args[])
{
word = "abc";
length = word.length();
num = new int[length + 1];
for(int i=1; i<=length-1; i++)
    num[i] = 0;
    num[length] = 1;        
    while(num[0] == 0)
    {
        display();
        System.out.println();
        increment(length);
    }
}
public static void increment(int digit)
{
    if(num[digit] + 1 <= length)
        num[digit]++;
    else
    {
        num[digit] = 1;
        increment(digit-1);
    }
}
public static void display()
{
    for(int i=1; i<=length; i++)
    {
        if(num[i] == 0)
            System.out.print(' ');
        else
            System.out.print(word.charAt(num[i]-1));
    }
}
}

I'm not sure about its complexity. But I don't think it has a high complexity.



来源:https://stackoverflow.com/questions/11477514/efficiently-creating-a-sequential-wordlist-from-charset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!