有效地创造,从字符集顺序词表 [英] Efficiently creating a sequential wordlist from charset

查看:126
本文介绍了有效地创造,从字符集顺序词表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一种有效的方式建立的从C / C给定的charset创建++顺序单词的列表。我给大家举一个例子:

如果字符集是ABC,算法应该输出:

  A
  b
  C
 AA
 AB
 AC
 巴
 BB
 公元前
 CA
 CB
 CC
AAA
AAB
...

我有一些想法,但都需要太多的数学,我真的需要一个快速的解决方案。谁是有一个想法?


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;字符* numToColumn(INT N,字符* outstr,为const char * baseset){
    的char * p = outstr;
    INT LEN;
    LEN = strlen的(baseset);
    而(N){
        * P ++ = baseset [0 +((N%LEN == 0)LEN:N%LEN) - 1];
        N =(N - 1)/ LEN;
    }
    * P ='\\ 0';
    返回strrev(outstr); // strrev不是ANSI C
}字符* incrWord(字符* outstr,为const char * baseset){
    字符* P;
    INT大小,LEN;
    INT I,携带= 1;    大小= strlen的(baseset);
    LEN = strlen的(outstr);
    对于(I = LEN-1;开展和放大器;&安培; I> = 0; - 我){
        INT POS;
        POS =和strchr(baseset,outstr [I]) - baseset; // MUST NOT NULL
        POS + = 1; //增量
        如果(POS ==大小){
            携带= 1;
            POS = 0;
        }其他{
            携带= 0;
        }
        outstr [I] = baseset [POS]
    }
    如果(进){
        的memmove(安​​培; outstr [1],&放大器; outstr [0],LEN + 1);
        outstr [0] = baseset [0];
    }
    返回outstr;
}诠释主(){
    为const char * CSET =ABC;
    字符的buff [16];
    INT I;    对于(i = 1; I< 16; ++ I)// 1原点
        的printf(%S \\ n,numToColumn(我,浅黄色,CSET));    的strcpy(BUFF,CC); //启动CC
    的printf(\\ n要重新启动\\ n%S \\ n,浅黄色);
    的printf(%S \\ n,incrWord(BUFF,CSET));
    的printf(%S \\ n,incrWord(BUFF,CSET));
    返回0;
}
/ *结果:
一个
b
C
AA
AB
AC

BB
公元前
CA
CB
CC
AAA
AAB
AAC重新开始
CC
AAA
AAB
* /

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?

解决方案

#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
*/

这篇关于有效地创造,从字符集顺序词表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆