Algorithm 基于任意字符串和长度生成字符组合-类似于排列

Algorithm 基于任意字符串和长度生成字符组合-类似于排列,algorithm,delphi,Algorithm,Delphi,这个问题以前在其他语言中被问过,但在搜索后没有用delphi。 看这个问题,这个问题,这个问题: 所以这个问题并不新鲜,但我很难将其翻译成德尔福 我想做的是生成包含重复字符的组合,例如: 如果我们有一个字符串(由用户指定):ABC,并且我们想要生成三个字符的长度(也是由用户指定的长度),我将得到: AAA AAB AAC ABB ABC ACA ACB ACB BAB BAC BAC等。 这段代码似乎可以做到这一点,但在C++中: int N_字母=4; 字符字母表[]={'a','b','c'

这个问题以前在其他语言中被问过,但在搜索后没有用delphi。 看这个问题,这个问题,这个问题: 所以这个问题并不新鲜,但我很难将其翻译成德尔福

我想做的是生成包含重复字符的组合,例如: 如果我们有一个字符串(由用户指定):ABC,并且我们想要生成三个字符的长度(也是由用户指定的长度),我将得到:
AAA AAB AAC ABB ABC ACA ACB ACB BAB BAC BAC等。

这段代码似乎可以做到这一点,但在C++中:

int N_字母=4;
字符字母表[]={'a','b','c','d'};
std::vector get_all_单词(int长度)
{
std::向量索引(长度,0);
向量词;
while(true)
{
std::字符串字(长度);
对于(int i=0;i
这似乎也做到了这一点:

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void displayPermutation(字符串置换[],整数长度){
int i;

对于(i=0;i,您展示的示例使这比必要的要复杂得多,至少在我看来是这样

你真正看到的是一个3位数,以3为基数的数字。你只需从0数到33=27,然后将每个数字转换为以3为基数的数字(使用“a”、“B”和“C”作为数字,而不是“0”、“1”和“2”)

在C++中,转换可以是这样的:

std::字符串cvt(int-in){
静态常数int base=3;
静态常数整数位数=3;
std::字符串ret;

对于(inti=0;i,这里是通过递归完成的(这篇文章的公认答案:

程序组合;
{$APPTYPE控制台}
{$R*.res}
使用
System.SysUtils;
过程displayPermutation(常量置换:字符数组;长度:整数);
变量
i:整数;
开始
对于i:=0到i长度-1 do
开始
如果我mod ILENGHT=0,那么
writeln(“”)
否则写入(置换[i]);
结束;
结束;
过程getPermutations(常量运算符银行:字符数组;运算符计数:整数;
置换:字符数组;置换长度,curIndex:整数);
变量
i:整数;
开始
//停止递归条件
如果(curIndex=置换长度),则
显示置换(置换,置换长度)
其他的
对于i:=0到运算符计数-1 do
开始
置换[curIndex]:=operatorBank[i];
getPermutations(运算符库、运算符计数、置换、,
置换长度,curIndex+1);
结束;
结束;
变量
运算符库,置换:字符数组;
i、 置换长度、curIndex、运算符计数:整数;
Q、 S:字符串;
开始
尝试
Q:='';
S:='';
而(Q'')和(S''是
开始
书面语(“”);
写('P(N,R)N=?:');
ReadLn(Q);
运算符计数:=长度(Q);
设置长度(运算符银行、运算符计数);
对于i:=0到运算符计数-1 do
运算符银行[i]:=Q[i+1];
写('P(N,R)R=?:');
ReadLn(S);
如果是S“”,则排列长度:=strotint(S)+1;
设置长度(置换、运算符计数);
curIndex:=0;
书面语(“”);
getPermutations(运算符库、运算符计数、置换、,
置换长度,curIndex);
结束;
除了
关于E:Exception-do
Writeln(E.ClassName,“:”,E.Message);
结束;
结束。

不完全按照您的输出顺序,而是按照类似于二进制数相加的顺序

0001 
0010 
0011 
0100 
...
其思想很简单:在数组中循环索引值,指示在相应位置使用哪个字符组成输出组合字符串。无需递归

NextComposition更新索引数组,以便定义下一个组合,只要未形成所有组合,则返回true。返回所有0时返回False

DefineCombinations接受包含要使用的字符(例如“ABC”)和组合字符串大小(例如:3)的字符串:这会将以下序列添加到备注中:

AAA, AAB, AAC, ABA, ABB, ABC, ACA, ACB, ACC, BAA, BAB, BAC, BBA, BBB, BBC, BCA, BCB, BCC, CAA, CAB, CAC, CBA, CBB, CBC, CCA, CCB, CCC
随心所欲地调整

function TForm1.NextCombination(var aIndices: array of Integer; const MaxValue: Integer): Boolean;

var Index : Integer;

begin
  Result:=False;
  Index:=High(aIndices);

  while not(Result) and (Index >= Low(aIndices)) do
  begin
    if (aIndices[Index] < MaxValue) then
    begin
      { inc current index }
      aIndices[Index]:=aIndices[Index] + 1;
      Result:=True;
    end
    else
    begin
      { reset current index, process next }
      aIndices[Index]:=0;
      Dec(Index);
    end;
  end;
end;

procedure TForm1.DefineCombinations(const Chars: String; const Size: Integer);

var aIndices     : array of Integer;

    Index        : Integer;
    sData        : String;

begin
     try
        SetLength(sData, Size);
        SetLength(aIndices, Size);

        repeat
           for Index:=Low(aIndices) to High(aIndices) do
             sData[Index + 1]:=Chars[aIndices[Index] + 1];

           memo1.Lines.Add(sData);
        until not(NextCombination(aIndices, Length(Chars) - 1));

     finally
        SetLength(aIndices, 0);
        SetLength(sData, 0);
     end;
end;
函数TForm1.nextcomposition(var aIndices:Integer数组;const MaxValue:Integer):布尔;
var指数:整数;
开始
结果:=假;
指数:=高(平均值);
而不是(结果)和(索引>=低(aIndices))做
开始
如果(aIndices[Index]

如果我想从中学习一些东西,请告诉我。< /P>如果你想学习,然后尝试把你所找到的一个实现翻译成Delphi。它不应该太难。如果你有特殊的问题,问具体的问题。C++代码很简单。你不懂什么?到目前为止你尝试了什么?请展示你的工作。你的动机是学习,所以让我们做一些。为你写一个德尔福翻译是没有帮助的。谢谢杰瑞,这是一个有趣的方法。这能奏效吗

AAA, AAB, AAC, ABA, ABB, ABC, ACA, ACB, ACC, BAA, BAB, BAC, BBA, BBB, BBC, BCA, BCB, BCC, CAA, CAB, CAC, CBA, CBB, CBC, CCA, CCB, CCC
function TForm1.NextCombination(var aIndices: array of Integer; const MaxValue: Integer): Boolean;

var Index : Integer;

begin
  Result:=False;
  Index:=High(aIndices);

  while not(Result) and (Index >= Low(aIndices)) do
  begin
    if (aIndices[Index] < MaxValue) then
    begin
      { inc current index }
      aIndices[Index]:=aIndices[Index] + 1;
      Result:=True;
    end
    else
    begin
      { reset current index, process next }
      aIndices[Index]:=0;
      Dec(Index);
    end;
  end;
end;

procedure TForm1.DefineCombinations(const Chars: String; const Size: Integer);

var aIndices     : array of Integer;

    Index        : Integer;
    sData        : String;

begin
     try
        SetLength(sData, Size);
        SetLength(aIndices, Size);

        repeat
           for Index:=Low(aIndices) to High(aIndices) do
             sData[Index + 1]:=Chars[aIndices[Index] + 1];

           memo1.Lines.Add(sData);
        until not(NextCombination(aIndices, Length(Chars) - 1));

     finally
        SetLength(aIndices, 0);
        SetLength(sData, 0);
     end;
end;