我需要code生成从pre指定的字符集,并在特定的位置开始,所以我可以停止再继续后面单词列表。我的code是不是很好,所以我真的AP preciate任何帮助,将其固定或暗示的使其更快/更高效或任何意见。
下面是输出电流:
数据= AAA
数据= AAB
数据= AAC
数据= ABA
数据= ABB
数据= ABC
数据= ACA
数据= ACB
数据= ACC
数据=一
数据=一
数据=一
数据=咩
数据= BAB
数据= BAC
数据= BBA
数据= BBB
数据= BBC
数据= BCA
数据= BCB
数据= BCC
数据= B
数据= B
数据= B
数据= CAA
数据=驾驶室
数据= CAC
数据= CBA
数据= CBB
数据= CBC
数据= CCA
数据= CCB
数据= CCC
数据= C
数据= C
数据= C
输出应该是(我猜)
数据= AAA
数据= AAB
数据= AAC
数据= ABA
数据= ABB
数据= ABC
数据= ACA
数据= ACB
数据= ACC
数据=咩
数据= BAB
数据= BAC
数据= BBA
数据= BBB
数据= BBC
数据= BCA
数据= BCB
数据= BCC
数据= CAA
数据=驾驶室
数据= CAC
数据= CBA
数据= CBB
数据= CBC
数据= CCA
数据= CCB
数据= CCC
,它们是3个字母组合的3个字母的字符集
的所有可能的组合和这里是我的C code
的#include< stdlib.h中>
#包括< stdio.h中>
#包括< WINDOWS.H>
#包括< stdbool.h>
字符*字符集=ABC;
INT LEN = 3;
字符*海峡;
INT CharPos(字符C)
{
INT I;
对于(i = 0; I< LEN;我++)
{
如果(字符集[我] ==三)休息;
}
如果(我==的strlen(字符集))i = 0;
返回我;
}
无效产生(INT POS)
{
而(海峡[0]!= 0)
{
布尔零= FALSE;
INT Y = 0;
而(STR [LEN - 1]!= 0)
{
的printf(数据=%S \ N,STR);
海峡[长度 - 1] =字符集[++ Y];
如果(零)
{
零= FALSE;
打破;
}
睡眠(100);
}
INT X = LEN;
而(X)
{
x--; // X = 1
如果(海峡[X]!= 0)
{
INT charpos = CharPos(STR [X]); //海峡[X] =一,charpos = 0
海峡[X] =字符集[++ charpos] // ABA
如果(STR [X] == 0)零= TRUE;
打破;
}
其他
{
海峡[X] =字符集[0];
}
}
海峡[长度 - 1] =字符集[0];
}
返回;
}
诠释的main()
{
海峡=的malloc(LEN);
的strcpy(STR,AAA);
产生(LEN - 1);
返回0;
}
解决方案
所以,你想要的是一个给定的字符串的所有可能的组合。
所以,我看了看它,写下你想要的序列中的特定字符串中的索引方面:
ABC
AAA = 000
AAB = 001
AAC = 002
ABA = 010
ABB = 011
ABC = 012
ACA = 020
ACB = 021
ACC = 022
咩= 100
...等等。
这基本上是从零开始计数到一些基本的一些数字。可能的组合数这里是3 ^ 3,所以你基本上计数至27日在基地3.更普遍的:设x是给定字符串的长度。我们接着从0计数到x ^ x的在基数X
您可以再使用重新presentation人数基数X找到n个组合。
我写在java中一个快速的解决方案:
静态无效的组合(字符串str){
INT基准= str.length();
INT组合=(int)的Math.pow(基地,基地);
的for(int i = 0; I<组合;我++){
INT C = I;
的char [] ARR =新的char [基地]
对于(INT J = arr.length-1,J> = 0; j--){
ARR [J] = str.charAt(三%的基础);
C / =基地;
}
的System.out.println(Arrays.toString(ARR));
}
}
组合(ABC)给了我:
AAA
AAB
AAC
ABA
ABB
ABC
ACA
ACB
ACC
咩
...
下面是一个在C可能的解决方案(首次使用C时):
的#include< stdio.h中>
#包括< string.h中>
#包括<文件math.h>
INT
主要() {
烧焦*海峡=ABC;
INT LEN =的strlen(海峡);
INT组合= LEN;
INT J = 1;
而(J ++< LEN){
组合=组合* len个;
}
INT I;
对于(i = 0; I<组合;我++){
焦炭ARR [LEN]
INT C = I;
诠释J;
为(J = LEN-1,J> = 0; j--){
ARR [J] = STR [C%LEN]
C / = LEN;
}
的printf(%S \ N,ARR);
}
返回0;
}
I needed code to generate a word list from a pre-specified character set and starting at a specific position so i can stop then continue later. My code isn't that good, so i would really appreciate any help fixing it or hints on making it faster/more efficient or any comments.
Here is the current output:
Data = aaa
Data = aab
Data = aac
Data = aba
Data = abb
Data = abc
Data = aca
Data = acb
Data = acc
Data = a
Data = a
Data = a
Data = baa
Data = bab
Data = bac
Data = bba
Data = bbb
Data = bbc
Data = bca
Data = bcb
Data = bcc
Data = b
Data = b
Data = b
Data = caa
Data = cab
Data = cac
Data = cba
Data = cbb
Data = cbc
Data = cca
Data = ccb
Data = ccc
Data = c
Data = c
Data = c
the output should be (i guess)
Data = aaa
Data = aab
Data = aac
Data = aba
Data = abb
Data = abc
Data = aca
Data = acb
Data = acc
Data = baa
Data = bab
Data = bac
Data = bba
Data = bbb
Data = bbc
Data = bca
Data = bcb
Data = bcc
Data = caa
Data = cab
Data = cac
Data = cba
Data = cbb
Data = cbc
Data = cca
Data = ccb
Data = ccc
which are all the possible combinations of the 3 letters character set of 3 letters combination
and here is my c code
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
char *charset = "abc";
int len = 3;
char *str;
int CharPos(char c)
{
int i;
for (i = 0; i < len; i++)
{
if (charset[i] == c) break;
}
if (i == strlen(charset)) i = 0;
return i;
}
void generate(int pos)
{
while(str[0] != 0)
{
bool zero = false;
int y = 0;
while (str[len - 1] != 0)
{
printf("Data = %s\n", str);
str[len - 1] = charset[++y];
if (zero)
{
zero = false;
break;
}
Sleep(100);
}
int x = len;
while (x)
{
x--; // x = 1
if (str[x] != 0)
{
int charpos = CharPos(str[x]); // str[x] = a, charpos = 0
str[x] = charset[++charpos]; //aba
if (str[x] == 0) zero = true;
break;
}
else
{
str[x] = charset[0];
}
}
str[len - 1] = charset[0];
}
return;
}
int main()
{
str = malloc(len);
strcpy(str, "aaa");
generate(len - 1);
return 0;
}
解决方案
So what you want is all possible combinations of a given string.
So I took a look at it, wrote down the sequence you wanted in terms of indexes in the given string:
"abc"
aaa = 000
aab = 001
aac = 002
aba = 010
abb = 011
abc = 012
aca = 020
acb = 021
acc = 022
baa = 100
... and so forth.
This is basically counting from 0 up to some number in some base. The number of possible combinations here is 3^3, so you are basically counting up to 27 in base 3. More generally: Let x be the length of the given string. We are then counting from 0 up to x^x in base x.
You can then use the number representation in base x to find the n-th combination.
I wrote a quick solution in java:
static void combinations(String str) {
int base = str.length();
int combinations = (int) Math.pow(base, base);
for (int i = 0; i < combinations; i++) {
int c = i;
char[] arr = new char[base];
for (int j = arr.length-1; j >= 0; j--) {
arr[j] = str.charAt(c % base);
c /= base;
}
System.out.println(Arrays.toString(arr));
}
}
combinations("abc") gives me:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
...
Here's a possible solution in C(first time using C):
#include <stdio.h>
#include <string.h>
#include <math.h>
int
main() {
char *str = "abc";
int len = strlen(str);
int combinations = len;
int j = 1;
while (j++ < len) {
combinations = combinations*len;
}
int i;
for(i = 0; i < combinations;i++) {
char arr[len];
int c = i;
int j;
for (j = len-1; j >= 0;j--) {
arr[j] = str[c % len];
c /= len;
}
printf("%s\n", arr);
}
return 0;
}