用的这个代码,生成所有包含元音字母(a e i o u)的5位字母,从早算到晚也没完成,
有没有更快的算法?      nlength:=5;
      for i:=0 to nlength-1 do  ArrayIndex[i]:=1; //密码指针arrayindex指针归0
      bNext:=true;
      while(bNext) do begin
        for i:=0 to nlength-1 do //处理密码的每一位
          cPass[i]:=David[ArrayIndex[i]];
        //cPass[nLength+1]:=#0;
        if (pos('a', Copy(PChar(cPass),1,Length(cPass)))>0) then //以元音a举例
        sl_arr[fn].Add(Copy(PChar(cPass),1,Length(cPass)));
        for j:=nlength-1 downto 0 do begin//密码指针arrayindex进位
          inc(ArrayIndex[j]);
          if(ArrayIndex[j]<>nDictCount+1) then break else //有效则break进行生成!
            ArrayIndex[j]:=1;  
          if(j=0) then bnext:=false;
        end;
        if terminated then bnext:=false;
        probar[fn].StepIt;
      end;

解决方案 »

  1.   

    用的这个代码,生成所有包含元音字母(a e i o u)的5位字母,从早算到晚也没完成,
    有没有更快的算法?procedure TForm1.Button1Click(Sender: TObject);
    const
      David='abcdefghijklmnopqrstuvwxyz';
      nlength=2;
      nDictCount=26;
    var
      i,j:integer;
      bNext:boolean;
      cPass:array[1..62] of char; //生成的密码
      ArrayIndex:array[1..62] of byte; //密码词典下标
      mysl:tstringlist;
    begin
      mysl:=tstringlist.Create;
      for i:=1 to nlength do  ArrayIndex[i]:=1;
      bNext:=true;
      while(bNext) do begin
        for i:=1 to nlength do  cPass[i]:=David[ArrayIndex[i]];
        cPass[nLength+1]:=#0;
        if pos('a', cPass)>0 then //以元音a举例
          mysl.Add(cPass);
        for j:=nlength downto 1 do begin
          inc(ArrayIndex[j]);
          if(ArrayIndex[j]<>nDictCount+1) then break
          else ArrayIndex[j]:=1;
          if(j=1) then bnext:=false;
        end;
      end;
      mysl.SaveToFile('1.txt');
    end;
      mysl.SaveToFile('1.txt');
    end;
      

  2.   

    const
      C_Char_0 = 'aeiou';implementation{$R *.dfm}
    function IncStr(str: string): string;
    var
      I, Len, ChLen: Integer;
      a: array [1..5] of Byte;
    begin
      ZeroMemory(@a, SizeOf(a));
      Len := Length(str);
      ChLen := Length(C_Char_0);
      for I := Len downto 1 do
      begin
        a[Len - I + 1] := Pos(str[i], C_Char_0);
      end;
      a[1] := a[1] + 1;
      for I := 1 to Len do
      begin
        if a[i] > ChLen then
        begin
          a[i] := a[i] - ChLen;
          a[i + 1] := a[i + 1] + 1;
        end;
      end;
      Result := '';
      for I := SizeOf(a) downto 1 do
      begin
        if a[i] <> 0 then
          Result := Result + C_Char_0[a[i]];
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      SBgn, SEnd: string;
      SL: TStringList;
    begin
      SEnd := 'uuuuu';
      SBgn := 'aaaaa';
      SL := TStringList.Create;
      SL.Add(SBgn);
      while SBgn < SEnd do
      begin
        SBgn := IncStr(SBgn);
        SL.Add(SBgn);
      end;
      //ShowMessage(SL.CommaText);
      SL.Free;
    end;
      

  3.   

    //速度很快
    procedure TForm1.Button1Click(Sender: TObject);
    var
      SBgn, SEnd: string;
      SL: TStringList;
    begin
      SEnd := 'uuuuu';
      SBgn := 'aaaaa';
      SL := TStringList.Create;
      SL.Add(SBgn);
      while SBgn < SEnd do
      begin
        SBgn := IncStr(SBgn);
        SL.Add(SBgn);
      end;
      SL.SaveToFile('c:\a.txt');
      SL.Free;
    end;
      

  4.   

    老大剑走偏锋,受教了。不过我要算的是包含5个元音字母1-5个的组合,比如apple
    您这个是每一位都是元音字母的组合,数量少多了哦
      

  5.   

    用你提供的代码运行在30秒内能运行完procedure TForm1.Button1Click(Sender: TObject);
    const
      David='abcdefghijklmnopqrstuvwxyz';
      nlength=5; //已经改成5
      nDictCount=26;
    var
      i,j:integer;
      bNext:boolean;
      cPass:array[1..62] of char; //生成的密码
      ArrayIndex:array[1..62] of byte; //密码词典下标
      mysl:tstringlist;
    begin
      mysl:=tstringlist.Create;
      for i:=1 to nlength do  ArrayIndex[i]:=1;
      bNext:=true;
      while(bNext) do begin
        for i:=1 to nlength do  cPass[i]:=David[ArrayIndex[i]];
        cPass[nLength+1]:=#0;
        if pos('a', cPass)>0 then //以元音a举例
          mysl.Add(cPass); //<<<<<<<<估计你原来的代码,这里写入的速度需要优化
        for j:=nlength downto 1 do begin
          inc(ArrayIndex[j]);
          if(ArrayIndex[j]<>nDictCount+1) then break
          else ArrayIndex[j]:=1;
          if(j=1) then bnext:=false;
        end;
      end;
      mysl.SaveToFile('1.txt');
    end;
      

  6.   

    mysl.Add(cPass); //<<<<<<<<估计你原来的代码,这里写入的速度需要优化
    后面省了一句: probar[fn].StepIt; 原来是画1000w次进度条消耗的时间。。